简体   繁体   中英

custom Toggle button in android

I have added a custom toggle button and it works fine. the issue is that for toggle button enable and disable. I need a different layout for button on ,button off ,button on & disabled ,button on & enabled ,button off & disable and button off & enabled. Here is code for on / off. How to do ??

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <bitmap android:src="@drawable/button_pause_pressed"
          android:gravity="center_vertical|center_horizontal" />
    </item>
    <item android:state_checked="false">
        <bitmap android:src="@drawable/button_play_pressed_new"
          android:gravity="center_vertical|center_horizontal" />
    </item>        
</selector>

I didn't do that before, but since the StateList is a drawable, it might be possible to reference another XML-File, containing a selector .

This would look like this...
button.xml :

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

    <item android:state_enabled="true"
    android:drawable="@drawable/button_enabled"/>

    <item android:drawable=@drawable/button_disabled/>
</selector>


button_enabled.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
    <bitmap android:src="@drawable/button_pause_pressed"
      android:gravity="center_vertical|center_horizontal" />
</item>
<item android:state_checked="false">
    <bitmap android:src="@drawable/button_play_pressed_new"
      android:gravity="center_vertical|center_horizontal" />
</item>        
</selector>

The button_disabled.xml would look like the button_enabled.xml , but containing the looks for the the disabled Button..

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM