android - Coordinator布局和权重

标签 android android-recyclerview android-coordinatorlayout

我需要创建一个具有线性布局的 Coordinatorlayout 和一个具有相同空间的 RecyclerView。

我尝试将layout_weight设置为0.5,但RecyclerView占用了所有空间。

这是我的代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@android:color/holo_green_light">

     </LinearLayout>

     <android.support.v7.widget.RecyclerView
        android:id="@+id/lvToDoList"            
        android:layout_weight="0.5"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </android.support.v7.widget.RecyclerView>

     <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@android:drawable/ic_input_add" />

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

我怎样才能实现这一目标?

问候,迭戈。

最佳答案

layout_weight 仅适用于 LinearLayout。因此,将 LinearLayout 作为中间层:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:background="@android:color/holo_green_light">
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lvToDoList"
            android:background="@android:color/holo_blue_bright"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            >
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@android:drawable/ic_input_add" />

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

关于android - Coordinator布局和权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008310/

相关文章:

java - 不兼容的 fragment 类型

android - RecyclerView不同的item装饰不同的item

android - 如何打包我的 Android 库,以便我的客户不会遇到像 "AAPT: error: attribute layout_behavior"这样的错误

android - 如何在 Fragment 上使用 onDraw?

android - 包装 RecyclerView 中的所有元素

android - Android Studio 是否向后兼容旧 API? (具体来说,需要针对 Android Studio 3.6.3 的旧 API (27))

android - 为什么 findFirstVisibleItemPosition 不能按预期工作?

android - 为什么这个自定义监听器为空?

java - 如何在不破坏 CoordinatorLayout.LayoutParams 的情况下重写 CoordinatorLayout.Behavior

android - 向下滚动到达 RecyclerView 顶部时如何扩展 AppBarLayout