如何在LinearLayout中包装协调器布局?

sta*_*pot 4 android android-layout coordinator-layout

我试图在另一个布局中使用新的Android Design Library的CoordinatorLayout.CoordinatorLayout包含RecyclerView.我试图做的是在CoordinatorLayout下面放置一个简单的LinearLayout.此布局应放在CoordinatorLayout下方.这是我使用的XML:

<LinearLayout 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"
    android:orientation="vertical">

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

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

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

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/conversation_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_send_white_36dp" />

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,我试图将CoordinatorLayout包装在另一个LinearLayout中.但是,在屏幕上,布局的第二部分甚至无法渲染.

Lam*_*rak 12

那是因为CoordinatorLayout有了layout_height="match_parent".这意味着它需要屏幕的整体大小和LinearLayout 渲染,但它是低于CoordinatorLayout或关闭屏幕.

一个简单的方法来解决这一问题将被设置weightCoordinatorLayout,以填补父布局,但留出空间necesseary显示页脚LinearLayout.这将是

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
Run Code Online (Sandbox Code Playgroud)

但这并不是理想的方式,所以我建议将其CoordinatorLayout作为根元素放置,并将它LinearLayout刚好RecyclerView放在它所属的位置.由于您的页脚LinearLayout具有固定高度,因此可以轻松完成

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">

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

    <android.support.v7.widget.RecyclerView
        android:paddingBottom="150dp" />

    <LinearLayout
        android:layout_height="150dp"
        android:layout_gravity="bottom">
        <EditText />
        <ImageButton />
    </LinearLayout>

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