如何使用扩展的SearchView显示溢出?

时间:2015-10-04 07:28:35

标签: android searchview

所以我使用以下代码在SearchView内展开了ActionBar

SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setIconifiedByDefault(false); //expand searchview

但这会使溢出菜单项消失。如何在展开SearchView的右侧显示它们?

enter image description here

menu.xml文件

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

<item
    android:id="@+id/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"
    android:title="Search"/>

</menu>

1 个答案:

答案 0 :(得分:0)

您必须设置app:showAsAction="always|collapseActionView"

删除searchView.setIconifiedByDefault(false); 并添加此

MenuItem searchItem = menu.findItem(R.id.action_search);
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
      MenuItemCompat.expandActionView(searchItem);