开发者

Change Background Color of TextView on Click

开发者 https://www.devze.com 2023-02-03 14:16 出处:网络
I need to change the background color of a TextView. the font color using ColorStateList what I can change, but t开发者_C百科he background color does not accept ColorStateList

I need to change the background color of a TextView.

the font color using ColorStateList what I can change, but t开发者_C百科he background color does not accept 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,
}
));

how to make the background color?

TextView control is created dynamically at runtime.

Thanks in advance.


You will need to set the backgroundDrawable for the TextView. I've only done my state lists in XML and it would be something like this:

<?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>

From what I understand, from the documentation if you want to do the state list in Java code you will need to use the StateListDrawable

0

精彩评论

暂无评论...
验证码 换一张
取 消