简体   繁体   中英

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:

<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. Using following code only "dSel" is shown and other tab images does not appear! 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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