Android设计库CoordinatorLayout,AppBarLayout和DrawerLayout

wuj*_*jek 16 android android-design-library androiddesignsupport

我在API 22上使用Android设计库.我想:

  1. 有一个Toolbar和一个DrawerLayout,里面有一个RecyclerView
  2. 让DrawerLayout低于工具栏; 例如,当工具栏可见时,抽屉的主要内容应位于其下方,(左)抽屉也应位于其下方,以便在展开时,工具栏仍然可见
  3. 滚动回收器视图时,将工具栏滚出屏幕

这甚至可能吗?我有嫁给#2和#3的问题.现在的方式是工具栏总是在抽屉布局上方,覆盖回收器中的第一个条目,以及左抽屉的顶部.这是我的布局文件(不完整,但显示我的结构):

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

    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"/>

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

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

        <Toolbar
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

看起来虽然RecyclerView的app:layout_behavior ="@ string/appbar_scrolling_view_behavior"设置没有效果,因为删除时,行为是相同的.

我尝试将RelativeLayout作为CoordinatorLayout的子项添加,以定义抽屉位于工具栏等下方,但似乎没有任何效果.

我正在努力实现库的可能性吗?

小智 11

如果要查看汉堡包图标和箭头的动画,请尝试以下操作.如果包含NavigationView的上边距(layout_marginTop),它将向下移动它.

<?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/navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways" />

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

        <!-- main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_menu"/>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)


The*_*eoK 0

开发者页面

DrawerLayout 充当窗口内容的顶级容器,允许从窗口边缘拉出交互式“抽屉”视图。

首先尝试将 DrawerLayout 放置为顶级容器(即父布局)。然后将 CoordinatorLayout 放在下面,看看会发生什么。

另外,您还没有添加 NavigationView。请在此处查看基本说明。