如何在不均匀高度的 CardView 之间提供相等的间距?

How to provide equal spacing between CardViews of uneven height?

我的屏幕上有 3 个垂直排列的卡片视图。卡的数量固定为3张,因为卡的内容不同,所以卡的高度也不同。我想平均划分卡片之间的间距,以便三张卡片平均占据整个屏幕 space 而不是第三张卡片和屏幕底部之间的大空白 space 如图所示下面(这是我现在的屏幕)。

我尝试在卡片视图之间提供 Space 元素,如下所示,但它没有做任何事情:

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </Space>

我也尝试将 weightSum 添加到父线性布局,但无济于事。有办法吗?

如果你想分割卡片之间的空间,你可以尝试使用带展开链的constraintLayout 或内展开链。 https://medium.com/@loutry/guide-to-constraintlayout-407cd87bc013

试试这个代码,我用 ConstraintLayout:

导入库:

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/card1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardBackgroundColor="@android:color/black"
        app:layout_constraintBottom_toTopOf="@id/card2"
        app:layout_constraintTop_toTopOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="100dp" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/card2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardBackgroundColor="@android:color/holo_orange_dark"
        app:layout_constraintBottom_toTopOf="@id/card3"
        app:layout_constraintTop_toBottomOf="@+id/card1">

        <View
            android:layout_width="match_parent"
            android:layout_height="200dp" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/card3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardBackgroundColor="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/card2">

        <View
            android:layout_width="match_parent"
            android:layout_height="200dp" />
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

所以你已经修复了 3 CardView:

  • 它的高度是wrap_content
  • 每张卡片都有顶部和底部的约束(第一张卡片粘在父顶部,最后一张卡片粘在父底部)

ConstraintLayout 负责平均拉出间距以匹配我们设置的所有约束。但请确保您的内容不要太长,顶部和底部会被剪掉。

结果:

您已经在那里了,但是不用 ​​Space,只需使用一个空视图来创建间距。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  />

<View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" /> 

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  />

<View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" /> 

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  />

<View 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />