我正在尝试从片段中的工具栏获取我的搜索视图。
我在on Createoptionsmenu中添加菜单项。
问题:searchItem.getActionView((返回"视图"而不是"搜索视图"。请参阅下面的代码
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = searchItem.getActionView(); // This one is red - "returns View, should return Searchview"
}
菜单项的XML(菜单_search.xml(:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/search"
android:id="@+id/search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
尝试以下:
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();