简体   繁体   English

Android操作栏菜单项

[英]Android actionbar menu items

I want to display a menu item 'refresh' in action bar (always shown). 我想在操作栏中显示菜单项“刷新”(始终显示)。 Other two menu items 'logout' and 'help' to displayed only when menu item is pressed. 其他两个菜单项“注销”和“帮助”仅在按下菜单项时显示。 My menu xml file is 我的菜单xml文件是

list.xml list.xml

   <item
    android:id="@+id/action_refresh"
    android:title="@string/refresh"
    android:icon="@drawable/ic_action_refresh"
    android:showAsAction="always"/>

<item 
    android:id="@+id/logout"
    android:title="@string/logout"
    android:showAsAction="never"
    />
 <item 
    android:id="@+id/help"
    android:title="@string/help"
    android:showAsAction="never"
    />

The problem is when I press menu button, it displays 'help' and 'logout' options. 问题是当我按菜单按钮时,它显示“帮助”和“注销”选项。 But again when I press menu button it gets added ie it shows two refresh button on action bar and two logout and help buttons on pressing menu. 但是再次按下菜单按钮时,它会被添加,即在操作栏上显示两个刷新按钮,在菜单上显示两个注销和帮助按钮。 It multiplies as I press menu button. 当我按菜单按钮时,它成倍增加。 Please help me! 请帮我!

My onCreateOptions: 我的onCreateOptions:

public boolean onOptionsItemSelected(MenuItem item) {

      switch (item.getItemId()) {
      case R.id.action_refresh:
                refreshing();
                break;
        case R.id.logout:
                logOutActivity();
                break;
        case R.id.help:
                helpActivity();
                break;
      }
      return false;       
      }

In my app I display another menu when a checkbox is clicked hence I used onPrepareOptionsMenu 在我的应用程序中,当单击复选框时,我显示另一个菜单,因此我使用了PrepareOptionsMenu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {


    return true;

}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(buttonChecked==true)
    {
        getMenuInflater().inflate(R.menu.listmenu, menu);
    }
    else
    {
        getMenuInflater().inflate(R.menu.list, menu);

    }
    return super.onPrepareOptionsMenu(menu);
}

your mistake is in this line. 您的错误在于这一行。 return your menu. 返回菜单。

return super.onPrepareOptionsMenu(menu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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