如何在Fragment类的工具栏中显示菜单项?

时间:2018-05-25 11:28:39

标签: java android xml layout android-toolbar

我在片段中添加了一个菜单,并希望在工具栏中显示菜单项。

菜单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/action_note"
        android:icon="@drawable/ic_more_24dp"
        app:showAsAction="always"
        android:visible="true"
        android:orderInCategory="1"
        android:title="Note"/>
    <item
        android:id="@+id/action_submit"
        android:icon="@drawable/ic_more_24dp"
        app:showAsAction="always"
        android:orderInCategory="2"
        android:title="Submit"/>
</menu>

保存包含片段的容器的视图如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardActivity">

    <include layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </include>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_above="@+id/navigationView"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        android:layout_marginTop="56dp"

       />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@color/cardview_dark_background"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/navigation_menu"
        />

</android.support.constraint.ConstraintLayout>

显示片段中菜单的代码如下:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
    init();
}

然后是菜单的代码

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_attendance, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.action_submit:

            return true;
        case R.id.action_note:

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

我添加了hasOptionMenu(true),但菜单项仍然不会显示在工具栏中

2 个答案:

答案 0 :(得分:1)

尝试替换此代码:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_attendance, menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_attendance, menu);
    return true;
}

答案 1 :(得分:0)

试试这个:在super.onCreateOptionsMenu(menu, inflater);方法中返回onCreateOptionsMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_attendance, menu);

    super.onCreateOptionsMenu(menu, inflater);
}