【问题标题】:What can I do to shrink the space between my CardViews within a RecyclerView?我可以做些什么来缩小 RecyclerView 中 CardView 之间的空间?
【发布时间】:2018-03-18 22:07:23
【问题描述】:

所以我正在学习 Android 应用程序开发,但我有点卡住了。我正在尝试使用 CardViews 制作 RecyclerView,但 CardViews 之间的空间太大。 This is what it looks like.

我希望 Google 应用在提要中具有更多外观。 This is more like what I'm shooting for.

无论如何,我已经在这个网站上搜索并搜索了我的问题的解决方案,但似乎没有任何东西对我有用。我希望有人能给我一些真正有用的东西。

这是我的卡片视图的 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp">

<android.support.v7.widget.CardView
    android:id="@+id/cv"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

        <TextView
            android:id="@+id/note_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="false"
            android:layout_below="@+id/event_time"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/note_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/note_title" />

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

【问题讨论】:

标签: android


【解决方案1】:

顶部和底部的空格是由您的填充引起的 android:paddingBottom="10dp",去掉这个或者减小它

【讨论】:

    【解决方案2】:

    减小根约束布局的 PadingBotom 你可以放 3 或 4 dp 而不是 10dp。

    android:paddingBottom="10dp"
    

    另外我不知道你的代码副本是否不好...... constraintLayout 必须在最后关闭: 在此之后:

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

    你必须有这个:

    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      来自the documentationCardView

      在 Lollipop 之前,CardView 在其内容中添加填充并在该区域绘制阴影。 ...如果您希望 CardView 在 Lollipop 平台和之后添加内部填充,您可以调用 setUseCompatPadding(boolean) 并传递 true

      在您的布局中,您的 CardView 标签上有此属性:

      app:cardUseCompatPadding="true"
      

      这与上面引用中的方法相同。

      因此,即使卡片的所有边距为零,由于内部填充行为,用户仍然会看到它们之间的空间。如果您想要真正紧密的间距,则必须删除此属性(当然,您仍然会在棒棒糖之前获得更大的间距)。

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 2017-09-04
        • 2016-12-28
        • 2021-11-12
        • 2016-03-13
        • 1970-01-01
        • 2014-02-10
        • 1970-01-01
        • 2012-10-11
        相关资源
        最近更新 更多