在Android的定制切换按钮 [英] custom Toggle button in android

查看:151
本文介绍了在Android的定制切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个自定义切换按钮,它工作正常。
问题是,切换按钮启用和禁用。
我需要一个不同的布局按钮,关闭按钮,按钮和放大器;禁用按钮和放大器;启用,关闭按钮和放大器;禁用和关闭按钮和放大器;启用。
这里是code开/关。
怎么办?

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:state_checked =真正的>
        <位图的android:SRC =@绘制/ button_pause_ pressed
          机器人:重力=center_vertical | CENTER_HORIZONTAL/>
    < /项目>
    <项目的android:state_checked =假>
        <位图的android:SRC =@绘制/ button_play_ pressed_new
          机器人:重力=center_vertical | CENTER_HORIZONTAL/>
    < /项目>
< /选择>


解决方案

我没有这样做之前,但由于的 StateList 的是一个可绘制的,有可能引用另一个XML的文件,包含的选择的。

这是这样的......结果
button.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <项目的android:state_enabled =真
    机器人:可绘制=@绘制/ button_enabled/>    <项目机器人:可绘制= @绘制/ button_disabled />
< /选择>

结果
button_enabled.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目的android:state_checked =真正的>
    <位图的android:SRC =@绘制/ button_pause_ pressed
      机器人:重力=center_vertical | CENTER_HORIZONTAL/>
< /项目>
<项目的android:state_checked =假>
    <位图的android:SRC =@绘制/ button_play_ pressed_new
      机器人:重力=center_vertical | CENTER_HORIZONTAL/>
< /项目>
< /选择>

button_disabled.xml 看起来像在 button_enabled.xml ,但包含查找残疾人按钮。

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..

这篇关于在Android的定制切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆