gpt4 book ai didi

禁用时Android TextView 颜色不会改变

转载 作者:IT老高 更新时间:2023-10-28 13:25:51 25 4
gpt4 key购买 nike

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

有什么想法吗?

最佳答案

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

在颜色文件中(例如 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 文件示例。

关于禁用时Android TextView 颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com