我試圖在我的應用程序中自定義ToggleButton
。我將背景設置爲9-patch圖像here。 然後在我的佈局XML:9補丁背景添加頂部和底部填充。如何避免它?
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/btn_toggle_bg"
android:checked="true"
android:gravity="center_horizontal|center_vertical" />
btn_toogle_bg.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+android:id/background"
android:drawable="@android:color/transparent"/>
<item
android:id="@+android:id/toggle"
android:drawable="@drawable/btn_toggle"/>
</layer-list>
btn_toggle.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_toggle_off" android:state_checked="false"/>
<item android:drawable="@drawable/btn_toggle_on" android:state_checked="true"/>
</selector>
9補丁看起來像這樣(btn_toggle_off
):
檢查狀態相同的圖像。
但是當我申請這個背景我得到一些意想不到的頂部和底部填充:
我已經得到了應用此背景時Button
或使用XML抽拉相同的結果作爲背景。 如何避免意外的填充?請幫助。
更新:
此外,當我加入這種風格應用程序的主題不存在填充但ToggleButton
becames無法點擊(不改變其狀態):
styles.xml
:
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
themes.xml
:
<style name="MyThemeName" parent="@android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
</style>
你確定這是填充或繪製並沒有看法?嘗試在帶有可繪製的容器中放置彩色視圖(而不是文本視圖),以檢查。 – 2013-03-24 14:08:15
@ User117我不確定,但請參閱更新。 – Sabre 2013-03-24 14:09:38