我在片段中看不到选项菜单

I don't see the option menu in the fragment

我正在制作 Bottom Navigation View

切换画面时显示片段

但是,当切换到Fragment时,右上角(Option menu)的icon不可见。

作为出于类似原因的解决方案,我添加了 setHasOptionsMenu(true),但选项菜单仍然不可见。

MainActivity.xml

<androidx.constraintlayout.widget.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=".activity.MainActivity">

    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>

fragment.java

public class WorkoutListFragment extends Fragment {
    RecyclerView rcv_dailyRecord;
    TextView mainNotifyText;
    Toolbar toolbar;
    LinearLayoutManager lm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_workout_list, container, false);
        initViews(rootView);
        setHasOptionsMenu(true);
        return rootView;
    }

    public void initViews(View v) {
        rcv_dailyRecord = v.findViewById(R.id.rcv_dailyrecord);
        toolbar = v.findViewById(R.id.toolbar);
        mainNotifyText = v.findViewById(R.id.main_notification_text);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {

        inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.record_item_add, menu);
    }
}

fragment.xml

<androidx.constraintlayout.widget.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=".activity.WorkoutListActivity">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp"
            android:theme="@style/Theme.AppBarOverlay">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="LIST"
                app:titleTextColor="@color/black"
                app:titleMarginStart="30dp"
                android:paddingRight="30dp"
                android:theme="@style/Theme.PopupOverlay"/>
        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

            <TextView
                android:id="@+id/main_notification_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="CHOOSE WORKOUT"
                android:textSize="16dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:textColor="@color/orgin_text_color"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcv_dailyrecord"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:scrollbars="vertical" />
        </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

menu.xml

<menu 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">
    <item
        android:icon="@drawable/ic_action_add"
        android:title="A D D"
        app:showAsAction="always" />
</menu>

尝试覆盖片段中的 onCreate 函数并在那里调用 setHasOptionsMenu 函数,如下所示:

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
 }

关于 onCreateonCreateView 之间的区别,您可以在 this Whosebug 线程中阅读更多内容。

试试这个,因为你的 Fragment 中有一个自定义工具栏, 只需将您的工具栏添加到您的 activity 和 setHasOptionMenu(true).

将此添加到您的 onCreate 方法中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_blank, container, false);
    initViews(rootView);


    ((AppCompatActivity) requireActivity()).setSupportActionBar((toolbar));
    setHasOptionsMenu(true);
    return rootView;
}