首页 > 解决方案 > 我在片段中看不到选项菜单

问题描述

我正在制作一个Bottom Navigation View.

切换屏幕时显示片段。

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

作为出于类似原因的解决方案,我添加了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>

片段.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);
    }
}

片段.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>

菜单.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>

标签: androidandroid-fragmentsoptionmenu

解决方案


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

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

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


推荐阅读