【发布时间】:2016-12-16 21:50:14
【问题描述】:
我的 Activity 中有一个片段,该片段有自己的工具栏。像这样:
这是片段的布局:
<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 工具栏上。
我在片段上做了这个:
在onCreate: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 上设置 menuItems?
【问题讨论】:
-
您只需要在片段的 onCreateView() 中添加 setHasOptionMenu(true) 即可。 stackoverflow.com/a/34082236/5722385
-
我看不到任何解决您描述的问题的答案。你设法让它工作吗?如果有,怎么做?
标签: android android-fragments menu toolbar