[英]android change tab icon
我想在状态发生变化时更改标签图标。 如果我使用以下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>
但是,现在我需要从数据/数据文件夹加载图像,并从这些图像生成可绘制的“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
最后我找到了解决方案。 每个州的真/假由“ - ”处理。
selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
selector.addState(new int[]{ -android.R.attr.state_selected }, d);
有关更多信息,请参阅此链接, Android:如何以编程方式更新选择器(StateListDrawable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.