SearchView.setIconifiedByDefault(false)
如果您android:showAsAction="always"
在菜单 xml 中设置了属性就足够了。
以下代码段对我有用:
default_options.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item android:id="@+id/action_search"
android:title="@string/search"
android:icon="@drawable/topbar_busqueda"
androidshowAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
活动
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.default_options menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
prepareSearchViewAdapter(searchView, menuItem);
super.onCreateOptionsMenu(menu, inflater);
}
private void prepareSearchViewAdapter(final SearchView searchView, MenuItem menuItem) {
searchView.setIconifiedByDefault(false);
}