【问题标题】:How to provide equal spacing between CardViews of uneven height?如何在高度不均匀的 CardView 之间提供相等的间距?
【发布时间】:2018-05-09 06:13:36
【问题描述】:

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

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

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

我还尝试将 weightSum 添加到父线性布局,但无济于事。有没有办法做到这一点?

【问题讨论】:

    标签: android android-linearlayout android-cardview


    【解决方案1】:

    你已经在那里了,但不是空间,而是使用一个空视图来创建间距。

    <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" /> 
    

    【讨论】:

    • 这就像一个魅力。你能告诉我为什么 不起作用吗?
    【解决方案2】:

    如果你想分割卡片之间的空间,你可以尝试使用带有散布链或散布链的约束布局。 https://medium.com/@loutry/guide-to-constraintlayout-407cd87bc013

    【讨论】:

      【解决方案3】:

      试试这个代码,我用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 负责平均拉出间距以匹配我们设置的所有约束。但要确保你的内容不要太长,顶部和底部会被剪掉。

      结果:

      【讨论】:

      • @Angel Koh 的回答有效。所以我接受它作为答案,因为它只需要很少的修改。我还没试过你的答案。与他的答案相比,你的答案有什么优势(比如性能)吗?
      • ConstraintLayout 对于复杂的布局很有效,可以帮助你避免很多嵌套的ViewGroup。在这种情况下,您只有 1 个方向约束,即垂直,使用 @Angel Koh 的答案会在性能上更好。直到:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多