根据公开片段更改选项在动作条菜单图标 [英] Changing options menu icon in actionbar depending on an open Fragment

查看:236
本文介绍了根据公开片段更改选项在动作条菜单图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个项目在我的选项菜单:

I have this item in my options menu:

<item
    android:id="@+id/opt_mnu_action"
    android:icon="@android:drawable/ic_dialog_info"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="New">
</item>

本身在主FragmentActivity创建的菜单。我想改变这个项目的图标编程取决于开放的片段,很明显,当用户点击这个按钮有不同的动作。我试过几件事情要做到这一点,但毫无效果。我想的最后一件事是在我的片段的方法onCreateView这code:

The menu itself created in main FragmentActivity. I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked. The last thing I tried was this code in my Fragment's onCreateView method:

MenuItem mi = (MenuItem) view.findViewById(R.id.opt_mnu_action);
mi.setIcon(R.drawable.ico_1);

但我的应用程序崩溃。那么,有没有办法做到这一点?

But my app crashed. So is there a way to do that?

的**更新**

**UPDATE**

下面就是我想现在要做的,都在我的主要主FragmentActivity:
首先我有一个菜单项action_button; 在我的层次视图。然后在我的 onCreateOptionsMenu 方法我将它实例:

Here's what I'm trying to do now, all in my main main FragmentActivity: First of all I have a MenuItem action_button; in my hierarchy view. Then in my onCreateOptionsMenu method I instantiate it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    action_button = menu.findItem(R.id.opt_mnu_action);
    return super.onCreateOptionsMenu(menu);
}

然后我创造了这个功能根据打开的标签更改图标:

Then I created this function to change the icon according to the open tab:

public void change_action_button_icon(int tab_position)
{
    switch(tab_position)
    {
    case 0:
        action_button.setIcon(R.drawable.ico_1);
        break;
    case 1:
        action_button.setIcon(R.drawable.ico_2);
        break;
    case 2:
        action_button.setIcon(R.drawable.ico_3);
        break;
    }
    invalidateOptionsMenu();
}

和我把它在我的 onTabSelected 方法:

public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
    setTab_position(tab.getPosition());
    change_action_button_icon(tab.getPosition());
}

但是,一旦我开始我的应用程序 - 它崩溃。我得到 NullPointerException异常错误在这行:

action_button.setIcon(R.drawable.ico_1);

我的猜测 - 这是因为图标变化前的要求action_button 被实例化。但我不知道怎么去克服它...

My guess - it happens because the icon change was requested before the action_button was instantiated. But I don't know how to overcome it...

推荐答案

使用此去的菜单项的引用:

Use this to get a reference to the menu item:

    menu.findItem(resourceId).setIcon(drawableId);

您必须把code改变onCreateOptionsMenu()图标。

You have to put the code to change the icon in onCreateOptionsMenu().

请参考下面我举的例子:

Please refer to my example below:

    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.option_menu, menu);

    if (needToChangeMenuItem){

        menu.findItem(resourceId).setIcon(drawableId);
    }

    manageMenuIcon(menu);

    needToChangeMenuItem = false;


    return true;
}


    public void manageMenuIcon(Menu menu){
    if (bluetoothIconOn){
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothon);
    } else
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothoff);

    if (gpsIconOn)
        menu.findItem(R.id.gps).setIcon(R.drawable.gps);
    else
        menu.findItem(R.id.gps).setVisible(false);

    if (slipAndDropIconOn)
        menu.findItem(R.id.fall).setIcon(R.drawable.fall);
    else
        menu.findItem(R.id.fall).setVisible(false);

    if (fesConnectIconOn)
        menu.findItem(R.id.fesConnection).setIcon(R.drawable.fesconnect);
    else
        menu.findItem(R.id.fesConnection).setVisible(false);


}


    public void changeMenuItem(int resId, int draId){


    needToChangeMenuItem = true;
    resourceId = resId;
    drawableId = draId;

    invalidateOptionsMenu();        


}

这篇关于根据公开片段更改选项在动作条菜单图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆