2011-01-07 231 views
3

我需要更改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控件是在运行时动态创建的。

在此先感谢。

回答

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

+0

嗨,作品完美,非常感谢你csaunders – seba123neo

+0

Arg。我没有意识到我的答案的其余部分被切碎了。我已经添加了必要的更改,如果你想在java代码中做到这一点。 – csaunders