简体   繁体   English

Android菜单项showAsAction =“always”不起作用

[英]Android menu item showAsAction=“always” doesn't work

I have tried setting the: 我试过设置:

android:showAsAction=".."

to every one of these: 对于以下每一个:

ifRoom, never, withText, always, collapseActionView

but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button. 但我总是得到相同的结果,操作栏上没有任何按钮,所以我必须按“菜单”按钮。

Here is a picture of the menu now : 这是现在菜单的图片:

当前菜单

<item android:id="@+id/smth1"
    android:title="@string/smth1"
    android:showAsAction="always"
    android:orderInCategory="1" />

I have even tried adding this: 我甚至试过添加这个:

android:uiOptions="splitActionBarWhenNarrow"

into application manifest file, but with no positive result (nothing changed). 进入应用程序清单文件,但没有正面结果(没有改变)。

I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result. 我尝试在各种API(14,16,17,19)上运行它,但结果相同。

If my question seems to be unclear, here is a picture of a menu, which I would like to have: 如果我的问题似乎不清楚,这里有一张菜单图片,我想要:

在此输入图像描述

Thanks for any help. 谢谢你的帮助。

You need to use the compatibility namespace ( see here ) 您需要使用兼容性命名空间( 请参阅此处

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

<item android:id="@+id/menu_add_size"
    android:title="@string/menu_add_item"
    android:icon="@android:drawable/ic_menu_add" 
    app:showAsAction="always"/>
</menu>

Once you're using that you can use as many menu buttons as will fit. 一旦你使用它,你可以使用尽可能多的菜单按钮。 You're no longer limited to just two buttons + overflow showing. 你不再局限于只有两个按钮+溢出显示。

You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method: 您可能只是没有足够的空间,您应该首先从您的Activity删除标题,方法是将其添加到您的onCreate方法:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar . 然后,您可以在此处看到,您可以显示的最大图标数量与屏幕宽度直接相关,在小型手机上,您只能在ActionBar看到2个图标。

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

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