【问题标题】:Replace toolbar layout according to the displayed fragment根据显示的片段替换工具栏布局
【发布时间】:2015-01-15 05:44:33
【问题描述】:

我有一个带有导航抽屉的活动,它替换了活动上的 main_fragment_container。 当显示其中一个片段时,我想更改工具栏的布局并为其添加一个微调器(并在片段隐藏时将其删除)。

我的布局是这样的:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<android.support.v7.widget.Toolbar

    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    sothree:theme="@style/AppTheme.ActionBar" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main layout -->
    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav drawer -->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.idob.mysoccer.ui.DrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start" />
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 这将使抽屉在工具栏下方打开。 The Navigation Drawer is specified in Material Design 与 ToolBar 重叠(但目前甚至 Google Play 都没有这样做)。
  • 我是故意这样做的,在我的设计中,抽屉在工具栏上方时看起来不太好。

标签: android navigation-drawer material-design android-toolbar


【解决方案1】:

不确定您要完成什么,但我认为,如果可能,您应该通过让片段自定义您的工具栏而不是替换它来解决此问题。您可以根据需要让片段在工具栏上隐藏/显示视图。

在片段OnCreateView()中添加setHasOptionsMenu(true);,然后覆盖onOptionsMenuCreated()

像这样:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.result_list, container, false);
}

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

如果您需要使用工具栏做更具体的事情,您可以使用

获取实例

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

【讨论】:

  • 我有两个不同的工具栏,带有标题的标准工具栏和带有微调器而不是标题的工具栏。我只想在显示我的片段之一时使用微调器显示到工具栏。
  • 我仍然建议编辑工具栏而不是替换。根据活动片段隐藏/显示您的标题和微调器。
  • 一个工具栏的实现效果很好!谢谢 请编辑您的答案,我会将其标记为已回答
  • 很好,很高兴我能帮上忙。我的回答明确建议编辑现有工具栏而不是替换它。这是第一句话。不确定要编辑什么?
  • 最后,解决方案是使用一个“完整”工具栏并根据显示的片段隐藏\显示相关微调器
【解决方案2】:

J.G.Sebring 的上述答案(已接受的答案)有效,但同时保留了 optionsMenu。以前的和新的。要保留为片段创建的新菜单,您可以添加 menu.clear()

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    menu.clear()
    inflater.inflate(R.menu.settings_menu, menu)
    super.onCreateOptionsMenu(menu, inflater)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多