我有一个ImageView,它有一个可绘制的图像资源设置到选择器.如何以编程方式访问选择器并更改突出显示和非突出显示状态的图像?
这是选择器的代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iconSelector">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/btn_icon_hl" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/btn_icon_hl" />
<!-- default -->
<item android:drawable="@drawable/btn_icon" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我希望能够替换btn_icon_hl和btn_icon其他图像.
我以编程方式将状态AddList"#e3bb87"中的StateListDrawable创建为StateListDrawable,但TextView.setTextColor不采用StateListDrawable(奇怪的是它在布局中工作)而是ColorStateList.我读了这个改变statelistdrawable文本颜色的android按钮
在ColorStateList的构造函数中,它只接受int数组
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{R.attr.state_pressed},
new int[]{R.attr.state_selected},
new int[]{-R.attr.state_selected},
},
new int[]{
Color.GREEN,
Color.BLUE,
Color.RED});
Run Code Online (Sandbox Code Playgroud)
color.xml中没有定义颜色,因为我下载了这个颜色属性.我怎么定义这样的?
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{R.attr.state_pressed}
},
**getThisColor**("#e3bb87"));
Run Code Online (Sandbox Code Playgroud)