简体   繁体   English

在 Android 中扩展 SearchView 后工具栏图标消失

[英]Toolbar icons disappearing after expanding the SearchView in Android

here's my problem:这是我的问题:

I have that nice toolbar with the icons in landscape mode:我有一个漂亮的工具栏,带有横向模式的图标: 在此处输入图片说明

after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):展开搜索视图并显示弹出菜单后,会出现“添加”项(我认为不应该):

在此处输入图片说明

then returning with the back arrow key, as you see, the add button goes:然后使用后退箭头键返回,如您所见,添加按钮变为:

在此处输入图片说明

and you won't find it in the popup menu anymore:你不会再在弹出菜单中找到它: 在此处输入图片说明

I'm using support:appcompat-v7:25.1.0, and here's my menu code:我正在使用 support:appcompat-v7:25.1.0,这是我的菜单代码:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/app_bar_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        android:enabled="true"
        android:visible="true"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item android:title="Add"
        android:enabled="true"
        android:icon="@drawable/ic_action_add"
        android:visible="true"
        app:showAsAction="ifRoom"
        android:id="@+id/add" />
    <item android:title="Settings"
        android:id="@+id/settings"
        app:showAsAction="never"
        android:icon="@drawable/ic_action_settings"
        android:enabled="true"
        android:visible="true" />
    <item android:title="Feedback"
        android:id="@+id/feedbvack"
        app:showAsAction="never"
        android:icon="@drawable/ic_action_feedback"
        android:enabled="true"
        android:visible="true" />

</menu>

I can set the add button showAsAction to "always" but I know that this is discouraged.我可以将添加按钮 showAsAction 设置为“始终”,但我知道这是不鼓励的。 Does anyone here know why is there this behavior?这里有人知道为什么会有这种行为吗? and how can I prevent that?我怎样才能防止这种情况发生?

Thanks in advance.提前致谢。

Try this 试试这个

<item
    android:id="@+id/app_bar_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:title="Search"
    app:showAsAction="always|collapseActionView"
    android:enabled="true"
    android:visible="true"
    app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
    android:enabled="true"
    android:icon="@drawable/ic_action_add"
    android:visible="true"
    app:showAsAction="always"
    android:id="@+id/add" />
<item android:title="Settings"
    android:id="@+id/settings"
    app:showAsAction="never"
    android:icon="@drawable/ic_action_settings"
    android:enabled="true"
    android:visible="true" />
<item android:title="Feedback"
    android:id="@+id/feedbvack"
    app:showAsAction="never"
    android:icon="@drawable/ic_action_feedback"
    android:enabled="true"
    android:visible="true" />

Set app:showAsAction to always to make sure it will be visible always. app:showAsAction设置为always确保它始终可见。

Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never 使用ifRoom具有此功能,如果它将显示可用空间,否则它将隐藏它更好地使用always或never

<item android:title="Add"
    android:enabled="true"
    android:icon="@drawable/ic_action_add"
    android:visible="true"
    app:showAsAction="always"
    android:id="@+id/add" />

You can try to use this: 您可以尝试使用此:

MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        supportInvalidateOptionsMenu();
        //or use invalidateOptionsMenu();
        return true;
    }
});

So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. 因此,当SearchView将被折叠时,工具栏将重新分配项目,并且ifRoom项目将可见。 I had this bug too and solved it this way. 我也有这个错误并以这种方式解决了。

@Kovy has answered above with the fix for this bug and I confirm it fixes the bug. @Kovy 已在上面回答了此错误的修复程序,我确认它修复了该错误。 Thank you very much, mate!非常感谢你,伙计! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener.但是,上述函数已被弃用,而支持单个菜单项 OnActionExpandListener。 Example of it is as posted below:它的示例如下所示:

MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            supportInvalidateOptionsMenu();
            return true;
        }
    });

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

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