是否可以仅通过拥有SearchView对象来获取ActionBar中的工具栏?
我知道您可以通过onCreateOptionsMenu()获得它,但在本例中,我只是想看看是否可以通过toolbar.findViewById获得它
发布于 2016-01-01 01:05:52
可以显示Yes.It的SearchView.For这是你需要添加的菜单功能,比如:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
default:
return super.onOptionsItemSelected(menuItem);
}
}你需要将下面的代码实现到menu_main菜单中(它必须留在菜单文件夹中)。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/></menu>希望能对你有所帮助,谢谢。
https://stackoverflow.com/questions/34548907
复制相似问题