编程方式替换选择图片 [英] Replace selector images programmatically
问题描述
我有一个具有设置为选择一个绘制的图像资源的ImageView的。我如何编程方式访问选择和改变的图像加亮和不突出的状态?
I have an ImageView that has a drawable image resource set to a selector. How do I programmatically access the selector and change the images of the highlighted and non-highlighted state?
下面是选择的code:
Here is a code of selector:
<?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>
我希望能够取代 btn_icon_hl
和 btn_icon
与其他图像。
推荐答案
据我已经能够找到(我试着做类似的东西我自己),有没有办法修改单个国家的StateListDrawable有后已经被定义。你可以,但是,通过code定义一个新的:
As far as I've been able to find (I've tried doing something similar myself), there's no way to modify a single state after the StateListDrawable has already been defined. You can, however, define a NEW one through code:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);
和你可以只让他们两个人的手,或者因为你需要它创建一个不同的。
And you could just keep two of them on hand, or create a different one as you need it.
这篇关于编程方式替换选择图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!