微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – android更改选项卡图标

我想在状态发生变化时更改标签图标.如果我使用以下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 : How to update the selector(StateListDrawable) programmatically

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐