我有NavigationView并添加了项,但是当我使用菜单文件在操作栏上添加搜索视图时,搜索项同时显示在该位置,并且我无法删除操作栏上的3个点,尝试从堆栈中获取所有答案溢出。找到以下代码,
activity_main_drawer.xml
<?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">
<group android:id="@+id/grp1">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/theme"
android:title="@string/nav_theme" />
</group>
<group android:id="@+id/grp2">
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/fingerprint"
android:title="@string/nav_touchid" />
</group>
<group android:id="@+id/grp3">
<item
android:id="@+id/nav_contact"
android:icon="@drawable/contact_us"
android:title="@string/nav_contacts" />
</group>
<group android:id="@+id/grp4">
<item
android:id="@+id/nav_manage"
android:icon="@drawable/notification"
android:title="@string/nav_notifications" />
</group>
<group android:id="@+id/grp5">
<item
android:id="@+id/nav_user_guide"
android:icon="@drawable/user_guide"
android:title="@string/nav_userguide" />
</group>
<group android:id="@+id/grp6">
<item
android:id="@+id/nav_logout"
android:icon="@drawable/logout"
android:title="Log Out" />
</group>
<item
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
在MainActivity.java中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
// Retrieve the SearchView and plug it into SearchManager
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
找到以下屏幕截图