2013-02-06 48 views
2

我跟着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> 

我已經驗證了自定義圖像是存在於繪項目目錄,所以很明顯無論是我誤解對方式的樣式級聯的東西,或者我在這個風格的瘋狂中錯過了一個參考。

回答

3

所以它似乎是唯一真正的問題是我的ToggleButton實施中缺少android:background元素;所以對上面代碼的唯一改變是添加該元素,例如,

<ToggleButton 
     android:id="@+id/profile_item_value_text" 
     style="@style/ProfileTagTheme" 
     android:background="@drawable/profile_btn_toggle_bg" 
     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" 
     android:onClick="onToggleClicked" /> 
1

我這樣做了幾個我的應用程序。請記住,這是一種完全不同的風格方法,可能會不適合您的情況。通過設置背景,我可以設置我想要的形象爲我的切換按鈕:

<ToggleButton 
    android:id="@+id/tb_useNotificationShortcuts" 
    android:layout_width="100dp" 
    android:layout_height="25dp" 
    android:layout_gravity="center" 
    android:background="@drawable/togglebutton" 
    android:tag="21" /> 


<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/custom_toggle_on" android:state_checked="true"/> 
    <!-- pressed --> 
    <item android:drawable="@drawable/custom_toggle_off"/> 
    <!-- default/unchecked --> 

</selector> 

的custom_toggle_on和custom_toggle_off是我的圖片,這些也可能是您的層名單。