如何在Android上更改切换按钮的背景颜色 [英] How to change the background color of the toggle button on Android

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

问题描述

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

I tried to change the background color of the toggle button using XML file as white color, but the toggle button is totally damaged. It looks like all the button was covered with white.

当我将切换按钮的颜色更改为白色时,切换按钮上没有ON或OFF的指示.是否有另一种更改背景的方式不会损坏切换按钮的指示?

There is no indication of ON or OFF on the toggle button when I have changed the color of the toggle button to white. Is there is another way to change the background which will not damage the indication of the toggle button?

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

This is how my XML code look for the toggle button.

推荐答案

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

Yes, there is a way to change the background as you wish, but you have to use a selector like this as background:

<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等(您可以使用颜色或创建渐变.请检查

For @Drawable, etc. (you can use a color or make a gradient. Check this for more information about gradients.

这篇关于如何在Android上更改切换按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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