简体   繁体   English

app:showAsAction ifRoom 不适用于 appcompat 操作栏

[英]app:showAsAction ifRoom is not working on appcompat action bar

I have an action bar with the following menu items;我有一个包含以下菜单项的操作栏;

<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>

The problem is my search button doesn't show on action bar but the text is showed in overflow.问题是我的搜索按钮没有显示在操作栏上,但文本显示在溢出中。 There's plenty of room in my action bar我的操作栏有足够的空间

I'm using "@style/Theme.AppCompat.Light"我正在使用"@style/Theme.AppCompat.Light"

Can anyone help me?谁能帮我?

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

In my case I had to add a few lines to onCreateOptionsMenu.就我而言,我不得不向 onCreateOptionsMenu 添加几行。

Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat. Android Studio 不允许我在使用 appCompat 时使用 android:showAsAction="ifRoom"。

app:showAsAction="ifRoom" wasn't working and I removed it without problems. 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);
    }

In case someone else gets into the same problem - I could not get the button to show up until I overrode the onCreateOptionsMenu function and Inflated the menu (I am using com.android.support:appcompat-v7:24.2.1 version)万一其他人遇到同样的问题 - 在我覆盖 onCreateOptionsMenu 函数并扩大菜单之前,我无法显示按钮(我使用的是 com.android.support:appcompat-v7:24.2.1 版本)

Here is the code snippet:这是代码片段:

 @Override
    public boolean onCreateOptionsMenu (Menu menu){

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

ToolBar (that is in ActivityBar) strives not to exceed some amount of visible elements. ToolBar(即在 ActivityBar 中)力求不超过一定数量的可见元素。 And that limit is lower than ToolBar can contain indeed.而且该限制确实低于 ToolBar 可以包含的范围。 The limit is set in android.support.v7.view.ActionBarPolicy class:限制在android.support.v7.view.ActionBarPolicy类中设置:

`/**
     * 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);

if (smallest > 600 || widthDp > 600 || (widthDp > 960 && heightDp > 720) || (widthDp > 720 && heightDp > 960)) { // For values-w600dp, values-sw600dp and values-xlarge. return 5; } else if (widthDp >= 500 || (widthDp > 640 && heightDp > 480) || (widthDp > 480 && heightDp > 640)) { // For values-w500dp and values-large. return 4; } else if (widthDp >= 360) { // For values-w360dp. return 3; } else { return 2; } }`

As you can see the limit is between 2 and 5, and it depends on the screen width.如您所见,限制在 2 到 5 之间,这取决于屏幕宽度。 So if you want to exceed the limit, you should use showAsAction="always" or create your own view for ActionBar .所以如果你想超过这个限制,你应该使用showAsAction="always"或者为 ActionBar 创建你自己的视图。

   app:showAsAction="ifRoom" 

改成

   android:showAsAction="ifRoom|withText"

You must have a namespace for the app declaration to work你必须有一个命名空间才能让应用声明工作

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

for the app namespace prefix to work in your action item.使应用程序命名空间前缀在您的操作项中工作。

app:showAsAction is an AppCompatActivity attribute. app:showAsAction是一个AppCompatActivity属性。 You could use androidx.appcompat.app.AppCompatActivity instead of android.view.MenuInflater .您可以使用androidx.appcompat.app.AppCompatActivity而不是android.view.MenuInflater Code sample could be as below代码示例如下

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

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

相关问题 app:showAsAction =“ ifRoom”不起作用 - app:showAsAction=“ifRoom” not working 当showAsAction总是或ifroom时,Android应用中的“共享”按钮不起作用 - share button in android app is not working when showAsAction is always or ifroom app:showAsAction =“ ifRoom”和android:showAsAction =“ ifRoom”均无法正常工作 - both app:showAsAction=“ifRoom” and android:showAsAction=“ifRoom” do not work as expected 没有在ActionBar中显示的动作项与showAsAction =“ifRoom” - Action Items not showing in ActionBar with showAsAction=“ifRoom” 浓咖啡-单击带有app:showAsAction =“ ifRoom”的菜单项 - Espresso - Clicking menu item with app:showAsAction=“ifRoom” 带有“ app:showAsAction =” ifRoom | collapseActionView“”的SearchView是否有错误? - Does SearchView with “app:showAsAction=”ifRoom|collapseActionView“” have bug? Android appcompat操作栏菜单项showAsAction无效 - Android appcompat actionbar menu item showAsAction not working 保留设置了showAsAction =“ ifRoom”的项目的orderInCategory - Preserve the orderInCategory of items having showAsAction=“ifRoom” set showAsAction =“ifRoom”的行为不应该如此 - showAsAction=“ifRoom” doesn't behave as it should 当showAsAction = ifRoom时,ShareActionProvider不起作用 - ShareActionProvider does not work when showAsAction=ifRoom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM