【问题标题】:How to wrap Coordinator Layout in a LinearLayout?如何将协调器布局包装在 LinearLayout 中?
【发布时间】:2015-09-04 02:48:26
【问题描述】:

我正在尝试在另一个布局中使用新的 Android 设计库的 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>

如您所见,我正在尝试将 CoordinatorLayout 包装在另一个 LinearLayout 中。然而,在屏幕上布局的第二部分甚至没有渲染。

【问题讨论】:

    标签: android android-layout coordinator-layout


    【解决方案1】:

    那是因为CoordinatorLayoutlayout_height="match_parent"。这意味着它占用了整个屏幕尺寸,并且您的LinearLayout 呈现,但它低于CoordinatorLayout 并在屏幕外。

    解决此问题的一种简单方法是将CoordinatorLayoutweight 设置为填充父布局,但留出必要的空间来显示页脚LinearLayout。这将是

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    

    但这不是理想的方式,所以我建议将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>
    

    【讨论】:

    • 这确实也是我最终所做的。我在底部放了一个固定的填充物。但是,我对这在低分辨率屏幕上的外观感到厌倦。
    • dp 单元设计精良,大部分工作正常。如果没有,您始终可以为不同的屏幕尺寸指定不同的dimensions
    • 我正在使用此代码,但除了 RecyclerView 之外,我有一个 FrameLayout,它在运行时由活动替换为 Fragments。片段中有 NestedScrollView、RecyclerView 等。但就我而言,LinearLayout (Bottom Footer) 位于应用栏的正下方,而不是屏幕的末尾。我试过layout_gravity &amp; layout_anchor`,但它粘在app_bar的底部
    • 你是用重量还是固定高度?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多