点击时更改TextView的背景颜色

3

我需要更改TextView的背景颜色。

使用ColorStateList可以更改字体颜色,但是背景颜色不接受ColorStateList。

lblEtiqueta.setTextColor (new ColorStateList (
new int [] [] {
new int [] {android.R.attr.state_pressed}
new int [] {android.R.attr.state_focused}
new int [0]
}, new int [] {
Color.rgb (255, 128, 192),
Color.rgb (100, 200, 192),
Color.White,
}
));

如何制作背景颜色?

TextView控件在运行时动态创建。

提前感谢。

1个回答

2

您需要为TextView设置backgroundDrawable。我只使用XML完成了我的状态列表,它将类似于以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="#00ff00" />
    </item>
    <!-- And so on -->
</selector>

据我的理解,如果您想在Java代码中创建状态列表,您需要使用StateListDrawable


啊,我没意识到我的回答剩下的部分被切掉了。如果你想用Java代码实现,我已经添加了必要的更改。 - csaunders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接