我已經將setIconified()
和setIconifiedByDefault()
定義爲false,但SearchView菜單項默認情況下不會擴展。這裏是我是如何實現它:如何使工具欄上的SearchView始終展開?
View customTitle = getLayoutInflater().inflate(R.layout.toolbar_custom_title, null);
toolbar.inflateMenu(R.menu.menu_buddies);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
SearchView searchView = (SearchView) toolbar.getMenu().findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Search Buddies");
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
我menu_buddies:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
xmlns:pawesome="http://schemas.android.com/apk/res-auto"
android:title="@string/action_search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="always|collapseActionView"
pawesome:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
安置自己的XML與'R.id.menu_search' –
@dieter_h已經發布的 –
可能的複製文件[展開,並搜索查看給自動對焦(http://stackoverflow.com/questions/11710042/expand-and-give-focus-to-searchview-automatically) – Sufian