且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-13 19:44:58

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

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.