禁用时Android文本视图颜色不会改变 [英] Android text view color doesn't change when disabled

查看:18
本文介绍了禁用时Android文本视图颜色不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为 TextView 对象调用 setEnabled(false) 时,文本颜色不会改变.我预计它将变为灰色.如果我删除 XML 文件中的 android:textColor 行,它会恢复正常.

When I call setEnabled(false) for a TextView object the text color doesn't change. I expected it will be changed to gray. If I remove the line of android:textColor in my XML file, it backs to normal.

有什么想法吗?

推荐答案

我认为发生的情况是,由于您覆盖了默认的 textcolor,它并没有继承其他 textcolor 样式.尝试为它创建一个 ColorStateList 并将 textColor 属性设置为它而不是颜色.

I think what's happening is that since you're overriding the default textcolor it isn't inheriting the other textcolor styles. Try creating a ColorStateList for it and setting the textColor attribute to it instead of to a color.

在颜色文件中(例如 res/color/example.xml):

In a color file (eg res/color/example.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/disabled_color" />
    <item android:color="@color/normal_color"/>
</selector>

然后在你的布局中:

<TextView
    android:text="whatever text you want"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/example" />

请注意,我已经有一段时间没有这样做了,而且我从记忆中输入了很多这样的内容,因此可能需要稍微调整一下.ColorStateList 文档(上面链接)有一个更充实的彩色 XML 文件示例.

Note, I haven't done this in a while and I'm typing a lot of this from memory, so it may need a little tweaking. The ColorStateList docs (linked above) have a more fleshed-out example for the color XML file.

这篇关于禁用时Android文本视图颜色不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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