How to set menu items in a fragment toolbar?

巧了我就是萌 提交于 2020-04-14 03:46:45

问题


I have a fragment in my Activity and the fragment has its own toolbar. Like this:

Image

Here is the layout of the fragment:

<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>

I want to add menu on the Setup_next_toolbar. Not on the MainActivity toolBar.

I did this on the fragment:

on the onCreate: setHasOptionsMenu(true);

then

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

but it added the menu on the MainActivity tolbar. How can I set menuItems on the Setup_next_toolbar?


回答1:


may be its too late, but just solved the same issue and think you would like to know. all you nee is just set up toolbar for activity

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

this will trigger onCreateOptionsMenu in fragment




回答2:


You can set an ImageView in Toolbar and open a popup menu when ImageView is clicked and them handle the menu items clicks in popup menu.

<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>

Inside fragment

@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();
        }
    });
}



回答3:


Try this:

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);
}


来源:https://stackoverflow.com/questions/41192921/how-to-set-menu-items-in-a-fragment-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!