本文介绍了禁用时,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样式。尝试为其创建,并将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文字视图的颜色不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 14:37