【问题标题】:How to set menu items in a fragment toolbar?如何在片段工具栏中设置菜单项?
【发布时间】:2016-12-16 21:50:14
【问题描述】:

我的 Activity 中有一个片段,该片段有自己的工具栏。像这样:

Image

这是片段的布局:

<FrameLayout 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="com.hometsolutions.space.Fragments.ControlFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/Setup_next_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_setup_next"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@+id/setup_next_recycler" />

    </LinearLayout>
</FrameLayout>

我想在 Setup_next_toolbar 上添加菜​​单。不在 MainActivity 工具栏上。

我在片段上做了这个:

onCreatesetHasOptionsMenu(true);

然后

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

但它在 MainActivity 工具栏上添加了菜单。如何在 Setup_next_toolbar 上设置 menuItems?

【问题讨论】:

  • 您只需要在片段的 onCreateView() 中添加 setHasOptionMenu(true) 即可。 stackoverflow.com/a/34082236/5722385
  • 我看不到任何解决您描述的问题的答案。你设法让它工作吗?如果有,怎么做?

标签: android android-fragments menu toolbar


【解决方案1】:

可能为时已晚,但刚刚解决了同样的问题并认为您想知道。您只需为活动设置工具栏

  ((MainActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);

这将在片段中触发onCreateOptionsMenu

【讨论】:

  • 这对我在onCreateViewonActivityCreatedFragment 中调用上述代码有帮助。但是想一想隐藏菜单图标的确切原因是什么。
【解决方案2】:

您可以在 Toolbar 中设置一个 ImageView,并在单击 ImageView 时打开一个弹出菜单,它们会处理弹出菜单中的菜单项点击。

<android.support.v7.widget.Toolbar
            app:layout_collapseMode="pin"
            android:fitsSystemWindows="false"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">


            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent>

                <ImageView
                    android:layout_marginRight="8dp"
                    android:id="@+id/overflow_menu"
                    android:clickable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    app:srcCompat="@drawable/overflow_menu"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

</android.support.v7.widget.Toolbar>

内部片段

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

 //Other stuff  

    ImageView overflowMenuImageView = view.findViewById(R.id.overflow_menu);
    overflowMenuImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popupMenu = new PopupMenu(getActivity(), view);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if(item.getItemId() == R.id.menu_item_id){

                        //Do your thing

                    }
                    return false;
                }
            });
            popupMenu.inflate(R.menu.my_menu);
            popupMenu.show();
        }
    });
}

【讨论】:

  • 它的工作。解决了我的问题。太感谢了。节省我的时间。
  • 哇,非常感谢这个答案,搜索 4 小时后找到
【解决方案3】:

试试这个:

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //TODO Add your menu entries here
  inflater.inflate(R.menu.menu, menu);
  super.onCreateOptionsMenu(menu, inflater);
}

【讨论】:

  • 这将适用于活动,但 getMenuInflater() 不适用于片段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 2016-06-09
  • 2016-07-15
  • 2020-03-24
  • 1970-01-01
相关资源
最近更新 更多