禁止在Android的文本视图颜色没有变化视图、文本、颜色、Android

2023-09-12 08:01:47 作者:明月下西楼

当我打电话的setEnabled(假)的一个TextView对象中的文本颜色没有变化。我预计它会变成灰色。如果我删除线:在我的XML文件Android的文字颜色,它备份到正常

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.

任何想法?

推荐答案

我觉得发生了什么事是,因为你覆盖默认的文本颜色它不继承其他文本颜色样式。尝试创建一个ColorStateList它和设置文字颜色属性它,而不是一种颜色。

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 /彩色/的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>

然后在您的布局:

then in your layout:

<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.