[英]How to wrap Coordinator Layout in a LinearLayout?
I´m attempting to use the new Android Design Library´s CoordinatorLayout inside of another layout. 我试图在另一个布局中使用新的Android Design Library的CoordinatorLayout。 The CoordinatorLayout contains a RecyclerView.
CoordinatorLayout包含RecyclerView。 What I´m attempting to do is to place a simple LinearLayout below the CoordinatorLayout.
我试图做的是在CoordinatorLayout下面放置一个简单的LinearLayout。 This layout should be placed below the CoordinatorLayout.
此布局应放在CoordinatorLayout下方。 This is XML I´m using:
这是我使用的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>
As you can see, I´m attempting to wrap the CoordinatorLayout inside another LinearLayout. 如您所见,我试图将CoordinatorLayout包装在另一个LinearLayout中。 However, on-screen the second part of the layout doesn´t even render.
但是,在屏幕上,布局的第二部分甚至无法渲染。
That is because the CoordinatorLayout
has layout_height="match_parent"
. 那是因为
CoordinatorLayout
有layout_height="match_parent"
。 That means it takes the whole size of the screen and your LinearLayout
is rendered but it is below the CoordinatorLayout
and off-screen. 这意味着它需要屏幕的整体大小和您
LinearLayout
呈现 ,但低于CoordinatorLayout
和关闭屏幕。
An easy way to fix this would be setting the weight
of the CoordinatorLayout
to fill the parent layout but leave space necesseary to display the footer LinearLayout
. 解决此问题的一种简单方法是设置
CoordinatorLayout
的weight
以填充父布局,但留出空间以显示页脚LinearLayout
。 This would be 这将是
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
But this is not ideal way so I would suggest leaving the CoordinatorLayout
as the root element and place your LinearLayout
just bellow the RecyclerView
where it belongs. 但这不是理想的方式所以我建议将
CoordinatorLayout
作为根元素,并将LinearLayout
放在它所属的RecyclerView
下面。 Since your footer LinearLayout
has fixed height this can be done easily 由于您的页脚
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.