【问题标题】:Coodinator Layout With Fragments / Toolbar not in the same XML带有片段/工具栏的协调器布局不在同一个 XML 中
【发布时间】:2015-07-02 22:35:41
【问题描述】:

你好,所以我想用工具栏来实现这一点,我看过很多教程,它们解释了如何在一个 XML 文件中使用所有布局。

但是,我不知道如何在下面的用例中解决这个问题

我有一个定义工具栏的 XML 文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:paddingTop="@dimen/app_bar_top_padding"
    app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

然后我将这个工具栏包含在一个活动布局(线性布局根)中,其中包含一个框架布局作为片段的容器

<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />

<FrameLayout
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >

现在这个片段是动态膨胀的,片段是一个回收器视图。 有人可以指出我如何将 CoordinatorLayout 与这种常见安排联系起来吗?

谢谢!

EDIT 已经解决了这个问题,但如果有错误请告诉我

我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include layout="@layout/toolbar"/>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.CoordinatorLayout>

    <include layout="@layout/navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

具有滚动标志的我的工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    android:theme="@style/ToolbarStyle"
    app:theme="@style/ToolbarStyle">
</android.support.v7.widget.Toolbar>

【问题讨论】:

    标签: android android-coordinatorlayout


    【解决方案1】:

    只要将 Toolbar 添加到 CoordinatorLayout 并包裹在 AppBarLayout 中,这应该不是问题。如果您尝试过但仍有问题,请展示您尝试过的代码,我们可以解决。

    【讨论】:

    • 您好,我解决了这个问题并忘记了这个问题:) 但我发布了代码以防我做错了。你能检查一下吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 2017-04-21
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多