[英]Change Background Color of TextView on Click
我需要更改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控件是在運行時動態創建的。
提前致謝。
您需要為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.