简体   繁体   English

android更改选项卡图标

[英]android change tab icon

I am trying to change tab icon when state changes. 我想在状态发生变化时更改标签图标。 All is working properly if I use drawable from following xml: 如果我使用以下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>

However, now I need to load images from data/data folder and drawables "d" and "dSel" are generated from these images. 但是,现在我需要从数据/数据文件夹加载图像,并从这些图像生成可绘制的“d”和“dSel”。 Using following code only "dSel" is shown and other tab images does not appear! 使用以下代码仅显示“dSel”,并且不显示其他选项卡图像! Thank you 谢谢

    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

Finally I found the solution. 最后我找到了解决方案。 True/false for each state is handled by "-". 每个州的真/假由“ - ”处理。

selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
selector.addState(new int[]{ -android.R.attr.state_selected }, d);

refer to this link for more information, Android : How to update the selector(StateListDrawable) programmatically 有关更多信息,请参阅此链接, Android:如何以编程方式更新选择器(StateListDrawable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM