【问题标题】:CardView Spacing between each separate card is non-existent每张单独的卡片之间的 CardView 间距不存在
【发布时间】:2018-03-05 15:29:41
【问题描述】:

您将在下面找到屏幕截图。如您所见,在我的 CardView 中的每张卡片之间,没有间距(它们相互堆叠在一起)The CardView

我试图在每张单独的卡片之间留出一个间距。 (每张卡片包含一个图像、一个项目编号和一个布尔值)

我已经尝试card_view:cardUseCompatPadding="true" 并手动添加填充。

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="16dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:id="@+id/cv"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        card_view:cardPreventCornerOverlap="true"
        card_view:cardUseCompatPadding="true"
        card_view:cardCornerRadius="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_alignParentTop="true"
                android:textSize="30sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_age"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_below="@+id/person_name" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

recyclerview_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:backgroundTint="#e6e6e6">

    <android.support.v7.widget.RecyclerView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/rv">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

【问题讨论】:

  • 很遗憾,没有任何改变。

标签: java android android-cardview cardview


【解决方案1】:

CardView 的父级中:LinearLayout 定义android:layout_margin="XXdp"。这可能会有所帮助。

【讨论】:

  • 我这样做了,但没有任何改变。
  • 你确定这是margin,而不是padding
  • 是的,我相信是的。只是为了确认这里是编辑后的代码。 &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp"&gt;
  • 我的错,应该是android:layout_margin
  • 那么,我到底应该怎么做呢?我有,android:layout_margin="10dp"
【解决方案2】:

你需要在widget.CardView中定义margin top:

android:layout_marginTop="2dp"

而且你不需要线性布局。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-10-04
  • 2017-09-04
  • 2022-01-01
  • 2020-05-23
  • 2018-03-20
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
相关资源
最近更新 更多