我有一个带有导航抽屉的活动。该活动包含一个可容纳3个片段的视图寻呼机。我已经成功地为菜单充气。但是,它没有显示在操作栏中,而是隐藏在三个点中。
我的菜单代码如下:
home_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_menu"
android:icon="@drawable/baseline_search_24"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
</menu>
onCreateOptionsMenu()代码:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = MenuInflater(this)
inflater.inflate(R.menu.home_menu, menu)
val searchItem: MenuItem? = menu!!.findItem(R.id.search_menu)
searchItem?.let {
it.actionView?.let {
val searchView: SearchView = it as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
}
}
return true
}
怎么了?
另一个菜单文件会影响该菜单吗?
这是另一个菜单文件:
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="@+id/admin_menu"
android:visible="true"
android:title="Admin Menu">
<menu>
<item
android:id="@+id/manage_user"
android:icon="@drawable/ic_group_gray_24dp"
android:title="Manage User" />
<item
android:id="@+id/manage_schedule"
android:icon="@drawable/baseline_calendar_today_24"
android:title="Manage Schedule" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_edit_profile"
android:icon="@drawable/baseline_edit_24"
android:title="Edit Profile" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/baseline_exit_to_app_24"
android:title="Logout" />
</group>
</menu>
bottom_navigation_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home_menu"
android:icon="@drawable/baseline_home_24"
android:title="Home"/>
<item
android:id="@+id/search_user_menu"
android:icon="@drawable/baseline_search_24"
android:title="Search User"/>
<item
android:id="@+id/schedule_request_menu"
android:icon="@drawable/baseline_inbox_24"
android:title="Request"/>
</menu>
答案 0 :(得分:0)
如果要显示菜单图标,请尝试此操作。
您应将showAsAction
属性添加到always
或ifRoom
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_refresh"
android:showAsAction="ifRoom"
android:title="@string/action_refresh"/>
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter"
android:showAsAction="always"
android:title="@string/action_filter"/>
</menu>