如何将菜单项显示为片段中工具栏上的动作

时间:2019-11-28 12:25:50

标签: android user-interface kotlin menu

我已经设置了一个基本菜单和一个工具栏,如下所示:

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
    }

}

通过这种简单的设置,我能够使工具栏正常工作。另外,点击菜单项“通知” ,就会发出预期的敬酒。

但是,菜单正像这样显示为下拉菜单:

menu_kt

我该如何采取行动?

编辑:

  • 我已使用属性.NoActionBar

  • 禁用了主题中的默认操作栏。 --

1 个答案:

答案 0 :(得分:1)

您在布局中使用本地Toolbar;即<Toolbar>。忽略了菜单XML中的app:showAsAction属性,因为它位于应用程序的名称空间中。

我想您实际上打算使用库Toolbar,在这种情况下,您想将该布局元素更改为<androidx.appcompat.widget.Toolbar>,并修改相关的import语句在您的HomeFragment类中。

如果出于某些原因您确实打算使用本机Toolbar,则需要在showAsAction名称空间中使用android属性;即android:showAsAction