【发布时间】:2020-03-24 02:41:51
【问题描述】:
我已经设置了一个基本菜单和一个工具栏,如下所示:
home_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>
fragment_home.xml
<?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>
HomeFragment.kt
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
}
}
通过这个简单的设置,我能够让工具栏正常工作。同样点击菜单项“通知”会按预期发出祝酒词。
但是,菜单显示为下拉菜单,如下所示:
如何将其转化为行动?
编辑:
我已经使用属性
.NoActionBar禁用了主题中的默认操作栏
-
【问题讨论】:
-
您希望如何显示在下拉菜单或操作栏上?
-
我想要一个通知按钮的图标。不下拉
-
您可以通过在菜单项中将 showAsAction 值设置为“ifRoom”来进行更改
标签: android user-interface kotlin menu