我创建了一个菜单,试图显示在“片段”中。如果片段包含在活动xml布局文件中,则一切正常。但是,如果我尝试通过片段事务将片段添加到frameLayout而不更改任何其他实现,则菜单将不会出现。
活动
Fragment frag = Fragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, frag)
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
片段
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater
inflater) {
inflater.inflate(R.menu.menu, menu);
}
活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_weight=".60"
android:layout_height="0dp">
<!--<fragment-->
<!--android:id="@+id/frag"-->
<!--android:name="name"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--</fragment>-->
</FrameLayout>
<FrameLayout
android:id="@+id/controls_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="@+id/controls"
android:name="ControlsFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
片段布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/softGray"
android:clickable="true"
android:focusable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="@style/collapsedText"
app:contentScrim="@color/softGray"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@style/expandedText"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="@string/action_groups">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
app:srcCompat="@drawable/ic_skyscraper" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_groups_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
菜单布局
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/search"
android:enabled="true"
android:icon="@drawable/ic_search_black_24dp"
android:title="@string/string"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/edit"
android:enabled="true"
android:icon="@drawable/ic_edit_black_alpha_24dp"
android:title="@string/string"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/add"
android:enabled="true"
android:icon="@drawable/ic_add_black_24dp"
android:title="@string/string"
app:showAsAction="ifRoom"
/>
</menu>