【问题标题】:How to change the background color of the toggle button on Android如何更改Android上切换按钮的背景颜色
【发布时间】:2011-12-01 05:31:50
【问题描述】:

我尝试使用 XML 文件将切换按钮的背景颜色更改为白色,但切换按钮已完全损坏。看起来所有的按钮都被白色覆盖了。

当我将切换按钮的颜色更改为白色时,切换按钮上没有任何指示。有没有其他方法可以改变背景,不会损坏切换按钮的指示?

<ToggleButton android:id="@+id/togglebutton"
              android:layout_width="100px"
              android:layout_height="46px"
              android:background="#ffffff"
              android:layout_above ="@+id/save"
              android:textOn="DAY"
              android:textOff="NIGHT" />

这就是我的 XML 代码查找切换按钮的方式。

【问题讨论】:

标签: android togglebutton


【解决方案1】:

是的,有一种方法可以根据需要更改背景,但是您必须使用像这样的选择器作为背景:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
    android:state_focused="true"
    android:state_pressed="false"
    android:drawable="@drawable/some_image" />
<item
    android:state_focused="true"
    android:state_pressed="true"
    android:drawable="@drawable/some_other_image" />
<item
    android:state_focused="false"
    android:state_pressed="false"
    android:drawable="@drawable/some_image1" />
<item
    android:state_focused="false"
    android:state_pressed="true"
    android:drawable="@drawable/other_image" />
</selector>

对于@Drawable 等(您可以使用颜色或制作渐变。有关渐变的更多信息,请查看this

【讨论】:

    【解决方案2】:

    按照这种方式使您的ToogleButton 在打开时具有红色背景,在关闭时具有绿色背景色

    首先,在drawable文件夹中创建tooglebutton_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/togglebutton_on"
            android:state_checked="true" />
        <item android:drawable="@drawable/togglebutton_off"
            android:state_checked="false"
            />
    </selector>
    

    第二,在drawable文件夹中创建togglebutton_on.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="#ff0000" /> // red color
    </shape>
    

    第三,在drawable文件夹中创建togglebutton_off.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="#00FF00" /> // green color
    </shape>
    

    最后,在你的 ToggleButton 中

     <ToggleButton
                android:id="@+id/btnMon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector
                />
    

    【讨论】:

    【解决方案3】:

    当您反编译 SystemUI.apk 时,您应该转到以下文件:SystemUI/res/values/colors.xml

    然后更改以下行:

    #ff000000 #ffffffff #80000000 #ffadc1d6 #ffffffff #ffe6e6e6

    【讨论】:

    • 一定有更简单的方法吗?
    • 我见过的最不寻常的解决方案)))
    猜你喜欢
    • 2011-03-05
    • 2017-08-19
    • 2021-02-10
    • 2013-06-07
    • 2012-09-05
    • 2021-08-29
    • 2021-01-25
    • 2021-07-05
    • 2021-04-27
    相关资源
    最近更新 更多