Change homeAsUpIndicator dynamically

北城余情 提交于 2019-12-12 00:28:33

问题


I build an app with four tabs and, as a consequence, four fragments. In the first fragment, I have a sliding menu and I throw with a custom icon in the actionbar. The problem is that in the others fragments, I want to stablish other custom icon to return to the first fragment but I don't know what to do...

This is my styles.xml with my custom icon for the sliding menu:

<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:homeAsUpIndicator">@drawable/menu32</item>
    </style>

The java code works well but I don't know how to change the icon dynamically.

This is what I have:


回答1:


You can use the following in your fragments:

Actionbar ab = getActivity().getActionBar();
ab.setIcon(icon);

Where icon is a Drawable or a resource id, when the up indicator is a part of your icon.
You can also use setHomeAsUpIndicator if you use API level > 11:

getActivity().getActionBar().setHomeAsUpIndicator(resId);

If your API level is lower, then I suggest you make use of ActionbarSherlock.



来源:https://stackoverflow.com/questions/20657463/change-homeasupindicator-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!