我跟着this example爲應用一些自定義主題,以我的切換按鈕,但是當我運行我的應用我看到通用的android切換圖標 - 我想我在這裏失去了一個環境或存在,並可能使用額外的眼睛就可以了。Android的切換按鈕自定義樣式沒有得到應用
我使用的佈局在ListView
爲此我想使用自定義可繪爲選中/取消選中狀態,通過默認勾選設置爲每次翻轉狀態:
<ToggleButton
android:id="@+id/profile_item_value_text"
style="@style/ProfileTagTheme"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@color/black"
android:layout_gravity="center"
android:gravity="center_vertical|center_horizontal"
android:checked="true" />
這裏就是我在我styles
文件ProfileTagTheme
:
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/profile_btn_toggle_bg</item>
<item name="android:disabledAlpha">@android:attr/disabledAlpha</item>
</style>
<style name="ProfileTagTheme" parent="android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
</style>
而反過來,profile_btn_toggle_bg.xml
,住在我的主要drawable
目錄:
<?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/profile_btn_toggle"/>
</layer-list>
它引用profile_btn_toggle
,就在旁邊:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/tag_active" /> <!-- pressed -->
<item android:drawable="@drawable/tag" /> <!-- default/unchecked -->
</selector>
我已經驗證了自定義圖像是存在於繪項目目錄,所以很明顯無論是我誤解對方式的樣式級聯的東西,或者我在這個風格的瘋狂中錯過了一個參考。