[英]Image set as background of toggle button is stretched android
我有一個切換按鈕:
<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"
/>
我的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>
我不明白為什么背景中的圖像被拉伸了?? 我試過各種尺寸的圖像。
您可以簡單地在 xml 布局文件中設置:
android:minWidth="0dp"
android:minHeight="0dp"
圖像將不再拉伸
<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="" />
在你的代碼上試試這個,只調整布局高度參數!
編輯
獲取非拉伸圖像的方法是使用位圖而不是可繪制對象。 使用以下作為您的 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>
在嘗試了幾件事后,我發現了問題所在:
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>
將整個代碼放在 LL 父級中就可以解決問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.