更改homeAsUpIndicator动态动态、homeAsUpIndicator

2023-09-12 03:30:01 作者:低调耍流氓

我建有四个选项卡的应用程序,并作为结果,4个片段。在第一个片段,我有一个滑动菜单和我扔在了动作条自定义图标。现在的问题是,在别人的片段,我想stablish其他自定义图标返回到第一个片段,但我不知道该怎么办......

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...

这是我的styles.xml与滑动菜单我的自定义图标:

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>

在Java code效果很好,但我不知道如何动态地更改图标。

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

这是我有:

推荐答案

您可以使用下面的在你的片段:

You can use the following in your fragments:

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

其中,图标是一个可绘制或资源ID,当向上的指标是你的图标的一部分。 您也可以使用 setHomeAsUpIndicator ,如果​​你使用的API级> 11

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);

如果你的API水平较低,那么我建议你使用ActionbarSherlock的。

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