【问题标题】:Reduce gap between card view item减少卡片视图项目之间的差距
【发布时间】:2019-09-04 13:32:16
【问题描述】:

我正在开发使用 CardView 和 RecyclerView 的应用程序。

我想减少两个卡片项目之间的空间。想指定cardview的宽度。(不想将宽度设置为MATCH_PARENT)

下面是我的代码 -

我已经尝试过 card_view:cardMaxElevation="1dp"card_view:cardElevation="1dp"card_view:contentPadding="-8dp",但这对我不起作用。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="15dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"/>

这是我的卡片视图 -

<android.support.v7.widget.CardView
        android:id="@+id/companyNameCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardBackgroundColor="@color/logo_yellow_light"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="3dp"
        card_view:cardUseCompatPadding="true"
        card_view:contentPadding="8dp">

        <RelativeLayout
            android:id="@+id/mainCardRelative"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/full_transparent_color">

            <RelativeLayout
                android:id="@+id/thumbnailRelative"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:layout_marginTop="5dp"
                android:layout_marginStart="5dp"
                android:background="@color/full_transparent_color">

                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:clickable="true"
                    android:contentDescription="@string/app_name"
                    android:focusable="true"
                    android:src="@drawable/ic_job" />

            </RelativeLayout>

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@+id/thumbnailRelative"
                android:background="@color/full_transparent_color"
                android:gravity="start"
                android:padding="5dp"
                android:text="@string/app_name"
                android:textColor="@color/black"
                android:textSize="13sp" />

        </RelativeLayout>

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

这就是我得到的

【问题讨论】:

  • 您确实将CardView 的宽度设置为match_parent。只需将其设置为wrap_content
  • 共享所有适配器视图 xml 文件
  • 为什么不在 Java 文件中动态设置高度?
  • @Vucko 我用 wrap_content 试过了,它不起作用..两个卡片项目之间的差距仍然相同
  • @TaslimOseni 我试过用DisplayMetrics displayMetrics = new DisplayMetrics(); this.activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels/7; int width = displayMetrics.widthPixels/2; RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) holder.card_view.getLayoutParams(); layoutParams.height= height; layoutParams.width= width; holder.card_view.setLayoutParams(layoutParams);进行设置

标签: android android-recyclerview android-cardview


【解决方案1】:

将您的两个项目包装在“LinearLayout”中以将它们彼此相邻。

<android.support.v7.widget.CardView
    android:id="@+id/companyNameCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="5dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardBackgroundColor="@color/logo_yellow_light"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="3dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="8dp">

    <LinearLayout
        android:id="@+id/mainCardRelative"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/full_transparent_color">

        <RelativeLayout
            android:id="@+id/thumbnailRelative"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginTop="5dp"
            android:layout_marginStart="5dp"
            android:background="@color/full_transparent_color">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:clickable="true"
                android:contentDescription="@string/app_name"
                android:focusable="true"
                android:src="@drawable/ic_job" />

        </RelativeLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/thumbnailRelative"
            android:background="@color/full_transparent_color"
            android:gravity="start"
            android:padding="5dp"
            android:text="@string/app_name"
            android:textColor="@color/black"
            android:textSize="13sp" />

    </LinearLayout>

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

【讨论】: