简体   繁体   English

android中的自定义切换按钮

[英]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 . 我之前没有做过,但是由于StateList是可绘制的,因此可能可以引用另一个包含选择器的 XML文件。

This would look like this... 看起来像这样...
button.xml : 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 : 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.. button_disabled.xml看起来像button_enabled.xml ,但是包含禁用Button的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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