如何在片段工具栏中设置菜单项? [英] How to set menu items in a fragment toolbar?

查看:102
本文介绍了如何在片段工具栏中设置菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有一个片段,该片段有自己的工具栏.像这样:

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

图片

这是片段的布局:

<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工具栏上.

我在片段上这样做了

:setHasOptionsMenu(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 上设置menuItem?

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

推荐答案

可能为时已晚,但只是解决了相同的问题,并认为您想知道.您所需要的只是设置活动工具栏

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

这将在片段中触发onCreateOptionsMenu

这篇关于如何在片段工具栏中设置菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆