动作栏中项目标题的动态更改

时间:2013-12-19 14:49:06

标签: java android android-actionbar

我要做的是:从onClick方法更改操作栏中项目的标题。 代码onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.option_menu, menu);    
    return super.onCreateOptionsMenu(menu);
}

我的option_menu.xml菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/state"
      android:title="@string/title_not_connected"
      android:showAsAction="ifRoom" />
</menu>

标题应改为下面课程中的onClick方法:

@SuppressLint("ValidFragment")
public class ReceiveSectionFragment extends Fragment {
        public NotificationCenter mNotificationCenter;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.receive, container, false);
            mNotificationCenter = new NotificationCenter();
            rootView.findViewById(R.id.bt_server_start)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MenuItem item = (MenuItem) menu.findItem(R.id.state);
                    item.setTitle(TAG);
                }
            });
    }
}

正如您所看到的,我正在尝试通过Id在菜单中找到此项目但我收到此错误:The method findItem(int) is undefined for the type R.menu。我正在使用片段和ReceiveSectionFragment使用R.layout.receive布局。

我该如何处理这个问题?可能,我使用错误的方法做错了。

我没有使用ActionBar Sherlock。

4 个答案:

答案 0 :(得分:4)

如果您想对ActionBar菜单进行更改,则必须致电InvalidateOptionsMenu

然后,您可以覆盖onCreateOptionsMenu以根据您的更改重建菜单。您无法在为构建菜单提供的方法之外编辑菜单。

每次拨打InvalidateOptionsMenu时,您的菜单都会通过onCreateOptionsMenu方法进行重建,因此您需要在那里进行所有更改。

http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()

http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)

无论您是否正在使用ActionBarSherlock,都是如此。

e.g:

设置变量以保存标题

String MyMenuTitle = "Default Menu";

你的OnClick

public void onClick(View view) {
    MyMenuTitle = "My New Menu Title";
}

您的菜单构建代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {

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

    MenuItem item = menu.findItem(R.id.state);
    item.setTitle(MyMenuTitle);

    return super.onCreateOptionsMenu(menu);
}

答案 1 :(得分:0)

您应该在要隐藏菜单的片段中插入以下行。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem item = (MenuItem) menu.findItem(R.id.your_action);
    item.setVisible(false);
}

答案 2 :(得分:0)

如果没有InvalidateOptionsMenu ..还有另一种方法可以做到这一点。

首先在onCreate方法之前将MenuItem声明为静态..

  static MenuItem item;

然后在onCreateOptionsMenu中初始化处理程序

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    item = menu.findItem(R.id.state);

    return true;
}

最后每当你想改变你的标题时都使用setTitle ......

 item.setTitle("your title");

我希望这会有所帮助......

答案 3 :(得分:0)

正在运作

静态MenuItem项; //定义全局变量

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.minu_filter, menu);
        menu.findItem(action_enter_manually).setVisible(true);
        // allocation id 
        item = menu.findItem(R.id.action_enter_manually);
    }

致电   item.setTitle(&#34;你的头衔&#34;);