状态更改时,我正在尝试更改选项卡图标。如果我从下面的xml中使用drawable,一切都可以正常工作:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/d_selected"
android:state_selected="true" />
<item android:drawable="@drawable/d_normal" />
</selector>但是,现在我需要从data/data文件夹加载图像,并从这些图像生成可绘制的"d“和"dSel”。使用下面的代码,只会显示"dSel“,而不会显示其他选项卡图像!谢谢
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
selector.addState(new int[]{ android.R.attr.state_pressed }, d);
selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
selector.addState(new int[]{ android.R.attr.state_focused }, d);
icon.setImageDrawable(selector);
//icon.setImageResource(drawableId); used with other method described, if related to xml发布于 2012-04-01 17:32:22
最后我找到了解决方案。每个状态的True/false由"-“处理。
selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
selector.addState(new int[]{ -android.R.attr.state_selected }, d);有关详细信息,请参阅此链接,Android : How to update the selector(StateListDrawable) programmatically
https://stackoverflow.com/questions/9959914
复制相似问题