Android - Textview 在状态改变时改变颜色

标签 android textview

如何在 TextView 的各种状态(聚焦、按下、启用)上应用颜色?

我已经提到了这个:http://developer.android.com/reference/android/content/res/ColorStateList.html ,但不知道如何将颜色状态列表应用于 TextView?或者有其他方法吗?

更新:

我想更改背景颜色。

最佳答案

在 res/color 目录下创建 xml。

示例文件名:selector_white_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="@color/Gray"/> <!-- pressed -->
    <item android:color="@color/White"/> <!-- default -->
</selector>

您可以添加更多状态。您可以使用“#ffffff”之类的颜色代码,而不是预定义的“@color/White”。 注意,使用 android:color 而不是 android:drawable。这个例子在按下它时会改变文本的颜色。将 textColor 属性设置为上面的选择器。

<TextView
       android:layout_width="wrap_content"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       android:textColor="@color/selector_white_gray"
       android:textSize="18sp" 
       android:textStyle="bold" >
</TextView>

关于Android - Textview 在状态改变时改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4336218/

相关文章:

Android 4.4 http api 错误?

java - 如何调试使用 maven 构建的 android 应用程序

java - Andengine视差背景

android - 从 TextView 文本中提取电话号码

字符串中的 Android 返回行 (\n)

objective-c - 从自定义类访问 IB 对象

java - 通过 Retrofit 中的模型类在 GET 请求 URL 中传递 ID

java - getText() 总是从动态创建的 Chip 组件返回空字符串?

android - getText 不可访问

android - 在 Android 中启用对讲时自动滚动 TextView