单击时更改TextView的背景颜色

时间:2011-01-07 14:23:49

标签: java android eclipse colors textview

我需要更改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 个答案:

答案 0 :(得分: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