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 :
<?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:
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 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.
<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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.