Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

android - 设置为切换按钮背景的图像被拉伸(Image set as background of toggle button is stretched android)

I have a Toggle Button as:

(我有一个切换按钮:)

<ToggleButton
            android:id="@+id/tv_pmpSwitch"
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_view"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textOn=""
            android:textOff=""
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_centerVertical="true"
            android:paddingTop="16dp"
            android:layout_marginLeft="16dp"
            /> 


And my toggle_view drawable is :

(我的toggle_view可绘制对象是:)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_list_action"
        android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_grid_action"
        android:state_checked="false"/>
</selector>


I don't understand why the image in background is stretched ??

(我不明白为什么背景图片会被拉伸?)

I've tried various size images.

(我尝试了各种尺寸的图像。)

  ask by AnswerDroid translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<ToggleButton
            android:id="@+id/tv_pmpSwitch"
            android:layout_width="0dp"
            android:layout_height="28dp"
            android:layout_weight="0.1"
            android:background="@drawable/toggle_view"                
            android:textOff=""
            android:textOn="" />

Try this on your code and adjust only layout height parameter!

(在您的代码上尝试此操作,并仅调整布局高度参数!)

EDIT

(编辑)

The way to get a non stretched image is using a bitmap and not a drawable.

(获取非拉伸图像的方法是使用位图而不是可绘制的。)

use following as your xml as your background.

(使用以下作为您的xml作为背景。)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <bitmap android:src="@drawable/ic_list_action"
          android:gravity="center_vertical|left" />
    </item>
    <item>
        <bitmap android:src="@drawable/ic_grid_action"
          android:gravity="center_vertical|left" />
    </item>
</layer-list>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...