Android 始终在支持操作栏中显示菜单项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22243405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:42:20  来源:igfitidea点击:

Show Menu item always in support action bar

androidandroid-actionbaroptionmenu

提问by Nitesh Kabra

enter image description hereI am working on android application . I have implemented supported action bar in it .I want to show option menu item always . But it is not showing . it is showing in drop down menu . my code for menu item given below.

在此处输入图片说明我正在开发 android 应用程序。我已经在其中实现了受支持的操作栏。我想始终显示选项菜单项。但它没有显示。它显示在下拉菜单中。我的菜单项代码如下。

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/add_post"
    android:title="@string/action_settings"

    />

And code of ActionBar Activity is given below:-

ActionBar Activity 的代码如下:-

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

any help will be appreciated.

任何帮助将不胜感激。

回答by fllo

If you use the AppCompatActionBar, you have to change your layout in this way:

如果使用AppCompatActionBar,则必须以这种方式更改布局:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/add_post"
        android:title="@string/action_settings"
        app:showAsAction="always" />
</menu>  

showAsActionattribute will work only with a custom prefixe, see the documentation. According to it:

showAsAction属性仅适用于自定义前缀,请参阅文档。根据它:

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

请注意,上面的 showAsAction 属性使用标记中定义的自定义命名空间。当使用支持库定义的任何 XML 属性时,这是必要的,因为这些属性在旧设备上的 Android 框架中不存在。

Hope this helps.

希望这可以帮助。

回答by Shubhank

From docsyou should add showAsActionattribute to alwaysor ifRoom

文档中,您应该将showAsAction属性添加到alwaysifRoom

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

回答by Rushabh Patel

You can use the different values of android:showAsActionproperty in android menu xml file..

您可以android:showAsAction在 android 菜单 xml 文件中使用不同的属性值..

to show all time visible menu on actionbar, you should go with android:showAsAction="always"..

要在操作栏上显示所有时间可见的菜单,您应该使用android:showAsAction="always"..

You can also use android:showAsAction="ifRoom"but it will show the menu in action bar not always but it will show according to different screen size.

您也可以使用,android:showAsAction="ifRoom"但它并不总是在操作栏中显示菜单,但它会根据不同的屏幕大小显示。

Edited

已编辑

Ref: Menu items doesn't show up on the actionbar

参考:菜单项未显示在操作栏上

Try with the below code then..

试试下面的代码然后..

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/menu_lang"
        app:showAsAction="always" <!-- app:showAsAction="ifRoom" -->
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>
</menu>

Hope it will help you..!!!

希望能帮到你..!!!

回答by Nauman Zubair

remove this line xmlns:app="http://schemas.android.com/apk/res-auto"

删除这一行 xmlns:app="http://schemas.android.com/apk/res-auto"

use android:showAsAction="always"instead of app:showAsAction="always"

使用android:showAsAction="always"代替app:showAsAction="always"

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_refresh"
    android:showAsAction="ifRoom"
    android:title="@string/action_refresh"/>

<item
    android:id="@+id/action_filter"
    android:icon="@drawable/ic_filter"
    android:showAsAction="always"
    android:title="@string/action_filter"/>
</menu>

回答by cren90

I realize this question is 2+ years old, but for completeness, if you want to set this in code use the following:

我意识到这个问题已经有 2 年以上的历史了,但为了完整起见,如果您想在代码中设置它,请使用以下内容:

menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

other options are

其他选项是

MenuItem.SHOW_AS_ACTION_NEVER
MenuItem.SHOW_AS_ACTION_IF_ROOM
MenuItem.SHOW_AS_ACTION_ALWAYS
MenuItem.SHOW_AS_ACTION_WITH_TEXT
MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW