Android 如何将菜单项显示为片段中工具栏上的操作
我设置了一个基本菜单和工具栏,如下所示:Android 如何将菜单项显示为片段中工具栏上的操作,android,user-interface,kotlin,menu,Android,User Interface,Kotlin,Menu,我设置了一个基本菜单和工具栏,如下所示: home\u menu.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"> <item android:id="@+id/n
home\u menu.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">
<item
android:id="@+id/notifications"
app:showAsAction="always"
android:title="@string/_ui_notifications"
android:icon="@drawable/ic_notifications_active"
/>
</menu>
HomeFragment.kt
您正在布局中使用本机工具栏
;i、 例如,
。这忽略了菜单XML中的app:showAsAction
属性,因为它位于应用程序的命名空间中
我猜您实际上是想使用库工具栏
,在这种情况下,您需要将布局元素更改为
,并修改HomeFragment
类中的相关导入
语句
如果出于某种原因,您确实打算使用本机
工具栏
,那么您需要使用android
命名空间中的showAsAction
属性;i、 例如,android:showAsAction
您希望如何在下拉列表或操作栏上显示?我想要一个通知按钮的图标。不下拉您可以通过将菜单项中的showAsAction值设置为“ifRoom”进行更改您正在使用布局中的本机工具栏
;i、 例如,
。这忽略了菜单XML中的app:showAsAction
属性,因为它位于应用程序的命名空间中。我猜您实际上是想使用库工具栏
,在这种情况下,您需要将其更改为
,并在HomeFragment
类中修改相关导入。如果出于某种原因,您确实打算使用本机工具栏
,那么您需要使用android
命名空间中的showAsAction
属性;i、 例如,android:showAsAction
。如果这不是问题@MikeM,我想去吻你。这就解决了问题。非常感谢。把它作为答案贴出来,我可以接受
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="._fragments.dash.HomeFragment"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="64dp">
<Toolbar
android:id="@+id/homeToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/app_logo_new"
android:scaleType="fitCenter"
android:padding="16dp"/>
</Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="240dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
appToolbar = view.findViewById(R.id.homeToolbar)
appToolbar.title = ""
appToolbar.inflateMenu(R.menu.home_menu)
appToolbar.setOnMenuItemClickListener{
when(it.itemId){
R.id.notifications -> {
Toast.makeText(activity!!, "Notifications", Toast.LENGTH_SHORT).show()
true
}
else -> {
Log.d("Menu", "Terrible things have happened in menu")
false
}
}
}
return view
}
}