通过xml更改切换按钮的文本颜色 [英] change toggle button text color through xml

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

问题描述

我正在尝试通过xml更改切换按钮文本的颜色

Hi i'm trying to change the color of toggle button's text through xml.

我已经引用了链接,但它只是更改了

I have referred links but its only changing the background color of toggle button but not its text.

我尝试过这种方法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:state_checked="false" android:color="#000000" />
</selector>

但只有背景在变化。

注意:我不想在代码中执行此操作,因为有21个切换按钮,并且为每个按钮设置侦听器都不好。

推荐答案

您不应将小部件样式的父项设置为主题。相反,您需要将其设置为要修改的默认窗口小部件样式(例如@android:style / Widget.Holo.Button.Toggle)。

You shouldn't set the parent of a widget style to be a theme. Instead, you'll want to set it to be the default widget style that you want to modify (e.g. @android:style/Widget.Holo.Button.Toggle).

但是,根据您的情况,您不需要使用样式:

In your case, however, you don't need to use a style:

res / color / toggle_text.xml:

res/color/toggle_text.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:color="#000000" />
</selector>

res / layout / your_layout.xml:

res/layout/your_layout.xml:

...
<ToggleButton
    android:id="@+id/toggleButton"
    ...
    android:textColor="@color/toggle_text" />

这篇关于通过xml更改切换按钮的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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