应用程序:showAsAction在appcompat操作栏上无法正常工作

10

我有一个带有以下菜单项的操作栏:

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

    <item android:id="@+id/action_search"
        android:icon="@drawable/search"
        android:title="@string/action_search"
        android:orderInCategory="1"
        app:showAsAction="ifRoom|withText"/>

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="5"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_waiter"
        android:title="@string/action_waiter"
        android:orderInCategory="6"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_cleantable"
        android:title="@string/action_cleantable"
        android:orderInCategory="7"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_suggest"
        android:title="@string/action_suggest"
        android:orderInCategory="8"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_waiterlogin"
        android:title="@string/action_waiterlogin"
        android:orderInCategory="9"
        app:showAsAction="ifRoom"/>
</menu>

问题是我的搜索按钮没有显示在操作栏上,但文本显示在溢出菜单中。 我的操作栏中有足够的空间。

我正在使用 "@style/Theme.AppCompat.Light"

有人能帮帮我吗?


我回答了一个类似于这个问题的问题:http://stackoverflow.com/a/22780579/2801779 - EdmDroid
@HipHopDroid,“app”命名空间已经存在。因此,实际上并不是同一个问题。 - aacanakin
7个回答

9
请尝试使用android:showAsAction="ifRoom|withText"而不是app:showAsAction="ifRoom|withText"

3
哇,真的成功了。我以为在使用AppCompat时,我们需要使用app:showAsAction。谢谢啊伙计! - aacanakin
3
在使用appcompat库时,菜单资源应该引用app:命名空间中的showAsAction,而不是android:命名空间。Android Studio报错信息。 - Mario Velasco
1
这个问题三年后还是同一个问题吗? - Daniel Wilson
1
android:showAsAction 会导致 Lint 错误,如果你使用 CI/CD,构建将会失败。唯一没有错误的方法是按照 Mario Eraso 的解决方案进行操作。 - Ali Kazi
意外的命名空间URI绑定到“android”前缀,是http://schemas.android.com/apk/res-auto,预期的是http://schemas.android.com/apk/res/android。 - Harikrushna Patel
显示剩余2条评论

7
在我的情况下,我必须在onCreateOptionsMenu中添加几行代码。
使用appCompat时,Android Studio不允许我使用android:showAsAction="ifRoom"。
app:showAsAction="ifRoom"无法工作,我没有遇到任何问题就将其删除了。
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater  inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        return super.onCreateOptionsMenu(menu);
    }

这是解决此问题的唯一方法。 - Ali Kazi

1

如果其他人遇到了同样的问题 - 直到我重写onCreateOptionsMenu函数并膨胀菜单(我正在使用com.android.support:appcompat-v7:24.2.1版本),我才能让按钮显示出来。

以下是代码片段:

 @Override
    public boolean onCreateOptionsMenu (Menu menu){

        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

0

ToolBar (that is in ActivityBar) strives not to exceed some amount of visible elements. And that limit is lower than ToolBar can contain indeed. The limit is set in android.support.v7.view.ActionBarPolicy class:

`/**
     * Returns the maximum number of action buttons that should be permitted within an action
     * bar/action mode. This will be used to determine how many showAsAction="ifRoom" items can fit.
     * "always" items can override this.
     */
    public int getMaxActionButtons() {
        final Resources res = mContext.getResources();
        final int widthDp = ConfigurationHelper.getScreenWidthDp(res);
        final int heightDp = ConfigurationHelper.getScreenHeightDp(res);
        final int smallest = ConfigurationHelper.getSmallestScreenWidthDp(res);

如果(smallest> 600 || widthDp> 600 ||(widthDp> 960 && heightDp> 720) ||(widthDp> 720 && heightDp> 960)){ //对于values-w600dp,values-sw600dp和values-xlarge。 返回5; } else if(widthDp> = 500 ||(widthDp> 640 && heightDp> 480) ||(widthDp> 480 && heightDp> 640)){ //对于values-w500dp和values-large。 返回4; } else if(widthDp> = 360){ //对于values-w360dp。 返回3; } else { 返回2; }

正如您所看到的,限制在2到5之间,并且它取决于屏幕宽度。因此,如果您想超出限制,您应该使用showAsAction =“always”或为ActionBar创建自己的视图。


0

app:showAsAction 是一个 AppCompatActivity 属性。你可以使用 androidx.appcompat.app.AppCompatActivity 代替 android.view.MenuInflater。代码示例如下:

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

0

在应用程序声明中必须有一个命名空间才能正常工作

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

为了让应用程序命名空间前缀在您的操作项中起作用。


-1
   app:showAsAction="ifRoom" 

更改为

   android:showAsAction="ifRoom|withText"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接