显示工具栏下的内容

Display content under toolbar

您好,我只是想将我的内容简单地放在工具栏下方,但是当我 运行 我的应用程序时,一些内容隐藏在它后面,而本应位于它下方。

我已经阅读了有关使用框架布局来尝试将其分开的信息,但我有点卡住了。我目前使用的是软件随附的基本 android 工作室导航抽屉模板,想知道我必须进行哪些更改。

我的协调器布局

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

我的抽屉布局

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

我需要做哪些改变?

app:layout_behavior="@string/appbar_scrolling_view_behavior"

将此代码添加到您的框架标签

许多 ViewGroup 允许其子项重叠。这些包括 FrameLayout、RelativeLayout、CoordinatorLayout 和 DrawerLayout。不允许其子项重叠的一种是 LinearLayout。

您问题的答案实际上取决于最终结果。如果您只想拥有一个始终显示在屏幕上的工具栏和它下面的一些内容,那么您根本不需要 CoordinatorLayout 和 AppBarLayout,您可以只使用带有两个子项的垂直 LinearLayout:

<LinearLayout android:orientation="vertical" ...>
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        ... />

    <FrameLayout 
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ... />
</LinearLayout>

注意 FrameLayout 的布局属性。

如果您想在滚动内容时工具栏在屏幕上和屏幕外滚动,那么您需要一个 AppBarLayout 并且您需要为您的内容区域提供一个特殊的属性,如下所示:

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

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll"
            ... />
    </android.support.design.widget.AppBarLayout>

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

正如@Brian Hoang 和@Karakuri 所说,使用 layout_behaviour 属性:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

似乎是一个很好的解决方案。即使您目前没有任何动画,但您计划在将来拥有,那么您仍然可以保留 CoordinatorLayout 和 AppBarLayout,以备将来添加动画。


根据我的理解,属性 的作用通常是计算整个 AppBarLayout UI 组件的高度。使用带@string/appbar_scrolling_view_behaviour layout_behaviour 属性 的 UI 组件将自动显示在 AppBarLayout 的正下方,无论高度是多少。

这样就不需要硬编码本应位于 AppBarLayout 下的 UI 中的任何上边距。

在下面的代码中,include_app_bar_with_tabs_layout (AppBarLayout) 的高度固定为 200dp(可以是 wrap_content 或其他任何值)。然后包含屏幕内容的 RelativeLayout 使用 layout_behaviour 属性.


查看下面的代码和 UI 图片:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include
        layout="@layout/include_app_bar_with_tabs_layout"
        android:layout_width="match_parent"
        <!-- this can be anything, even wrap_content -->
        android:layout_height="200dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/Green"
        <!-- this tells it to be below the include_app_bar_with_tabs_layout (AppBarLayout) -->
        app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adView" />

        <com.google.android.gms.ads.AdView
            android:id="@id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            app:adSize="BANNER"
            app:adUnitId="@string/banner_ad_unit_id"
            tools:background="@color/green"
            tools:layout_height="50dp" />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>