繁体   English   中英

如何更改Android上切换按钮的背景颜色

[英]How to change the background color of the toggle button on Android

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

将切换按钮的颜色更改为白色时,切换按钮上没有ON或OFF的指示。 是否有另一种方法可以改变背景,不会损坏切换按钮的指示?

<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代码查找切换按钮的方式。

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

<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等(您可以使用颜色或制作渐变。请查看此内容以获取有关渐变的更多信息。

按照这种方式使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
            />

在对SystemUI.apk进行反编译时,应该转到以下文件:SystemUI / res / values / colors.xml

然后更改以下行:

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

暂无
暂无

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

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