android更改选项卡图标

时间:2012-03-31 21:37:43

标签: java android eclipse

我正在尝试在状态更改时更改标签图标。如果我使用以下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

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案。每个州的真/假由“ - ”处理。

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

相关问题