如何在 Recylerview 中的 CardView 周围创建一些边距

问题描述 投票:0回答:1

我有以下用于 cardView 的 XML 布局文件,然后将其放入 Recylerview 中:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardView_itemLevel"
    android:layout_width="150dp"
    android:layout_height="150dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="10dp"
    android:layout_marginTop="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginBottom="12dp"
    android:layout_marginEnd="12dp"

    app:cardPreventCornerOverlap="true"


    app:cardElevation="0dp"
    app:cardUseCompatPadding="false"
    app:contentPaddingBottom="2dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/imageView_levelImage"
            android:layout_width="75dp"
            android:layout_height="75dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.507"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="5dp"
            />

        <TextView
            android:id="@+id/textView_bestResultThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:textSize="10sp"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView_levelImage" />


        <TextView
            android:id="@+id/textView_CO2SavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_bestResultThisLevel" />


        <TextView
            android:id="@+id/textView_gasSavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_CO2SavingsTotalThisLevel" />



    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

我使用所有方向的边距值,但输出仍然如下所示:

不幸的是,在垂直维度上(片段始终以横向模式显示),不同卡片视图周围没有边距。我的问题是如何创建这样的边距。

android android-recyclerview android-cardview
1个回答
0
投票

将卡片视图添加到另一个布局。

<RelativeLayout
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="wrap_content"
android:padding="2.5dp">

<androidx.cardview.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardBackgroundColor="@color/tintBackground"
    app:cardCornerRadius="8dp"
    app:cardElevation="0dp">

<ImageView
    android:id="@+id/image_view_center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:src="@drawable/use_default_icon"/>

</androidx.cardview.widget.CardView>

 </RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.