[英]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 drawable是:
<?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.
我试过各种尺寸的图像。
You can simply set in the xml layout file:您可以简单地在 xml 布局文件中设置:
android:minWidth="0dp"
android:minHeight="0dp"
The image will not stretch anymore图像将不再拉伸
<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>
After trying out a few things I found what was wrong:在尝试了几件事后,我发现了问题所在:
The weightSum was the culprit and the assigned weight was stretching the image. weightSum 是罪魁祸首,分配的权重会拉伸图像。
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_pmpSwitch"
android:background="@drawable/ic_toggle_list"
/>
</LinearLayout>
Putting the whole code inside a LL parent did the trick将整个代码放在 LL 父级中就可以解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.