Меньшее пространство между просмотром карты в списке - PullRequest
0 голосов
/ 20 января 2020

Я пытаюсь уменьшить пространство между карточками в списке; в настоящее время пространства слишком велики. Я везде смотрел на SO, но пока, похоже, нет хорошего решения. Я не могу понять правильный макет для достижения этого:

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="100dp"


        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="5dp"
        card_view:cardCornerRadius="30dp"
        card_view:contentPadding="5dp">


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

        <TextView
                android:id="@+id/sub_txt"
                android:textSize="18sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="italic"
                android:layout_toLeftOf="@+id/img"
                android:layout_below="@+id/txt"
                android:layout_marginStart="20dp"
                />
        <ImageView android:layout_width="30dp"
                   android:layout_height="wrap_content"
                   android:id="@+id/img"
                   android:contentDescription="@string/app_name"/>

        <TextView
                android:id="@+id/txt"
                android:textSize="22sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_toRightOf="@+id/img"
                android:layout_marginStart="20dp"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

enter image description here

Ответы [ 4 ]

3 голосов
/ 20 января 2020

Попробуйте удалить cardUseCompatPadding и добавить margin вместо этого, как показано ниже:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="100dp"

    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"

    card_view:cardElevation="5dp"
    card_view:cardCornerRadius="30dp"
    card_view:contentPadding="5dp">
0 голосов
/ 20 января 2020

CardView по умолчанию добавляет дополнительные отступы в платформы pre-Lollipop, чтобы рисовать тени. В устройствах Lollipop вам нужно установить cardUseCompatPadding=true, используйте строку ниже.

card_view:cardUseCompatPadding="true" 
0 голосов
/ 20 января 2020

Измените CardView высоту на wrap_content, а затем вы можете установить RelativeLayout высоту на match_parent.

Вы дали 100dp на CardView и RelativeLayout высота меньше той, что приводит к большему расстоянию между предметами.

0 голосов
/ 20 января 2020

Попробуй сделать содержание карты высотой

android:layout_height="wrap_content"
...