【问题标题】:How do I decrease the space between cards in a CardView within a RecyclerView?如何减少 RecyclerView 中 CardView 中卡片之间的空间?
【发布时间】:2017-09-04 03:03:03
【问题描述】:

我正在为我的应用程序的主屏幕设计布局。我正在使用带有网格布局的 RecyclerView 和其中的 CardView。我正在尝试减少每张卡之间的空间。下面是一个屏幕截图(打开了布局轮廓)以及我的布局文件。我怎样才能让每张卡片在垂直和水平方向上更接近一点?我一直在使用边距,但似乎找不到合适的边距或填充值来执行此操作。

thumbnail_view.xml:

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

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardMaxElevation="15dp"
    card_view:cardElevation="10dp"
    card_view:contentPadding="5dp"
    android:padding="1dp"
    card_view:cardBackgroundColor="@color/colorPrimary"
    card_view:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/card_list">
        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:contentDescription="@string/video_thumbnail_description"
            android:layout_weight="1"
            />

        <TextView
            android:id="@+id/video_title_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_weight="2"
            />
    </LinearLayout>
</android.support.v7.widget.CardView>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.int_a.giantbombforandroid.main.mainscreen.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

【问题讨论】:

  • 您是否尝试将您的CardView 的宽度更改为wrao_content
  • 在您建议之前我没有尝试过,但这并没有什么不同。另外,我希望每张卡片的尺寸相同,所以我不认为我想要wrap_content。不过还是谢谢。

标签: android xml android-layout android-recyclerview android-cardview


【解决方案1】:

编辑这些属性

card_view:cardMaxElevation="1dp"
card_view:cardElevation="1dp"

所以完整的代码将是

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

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardMaxElevation="1dp"
    card_view:cardElevation="1dp"
      card_view:cardBackgroundColor="@color/colorPrimary"
    card_view:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/card_list">
        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:src="@drawable/bag"
            android:contentDescription="string/video_thumbnail_description"
            android:layout_weight="1"
            />

        <TextView
            android:id="@+id/video_title_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="lovelksdjslkdjlsdj"
            android:layout_weight="3"
            />
    </LinearLayout>
</android.support.v7.widget.CardView>

【讨论】:

  • 谢谢!那行得通。有点奇怪的是,海拔控制着卡片之间的间距。我认为这与页面外的感知高度有关,以影响阴影和偏移等内容。
  • @intA 高程在 Lollipop 下不受支持。为兼容阴影添加了额外的空间。更大的潜在阴影 (maxCardElevation) 意味着分配的空间更多。水平方向为1*maxCardElevation,每侧垂直方向为1.5*maxCardElevation。默认卡片高度为 2dp。
  • @intA app:cardUseCompatPadding="true" 确保在 Lollipop 上使用相同的算法和分配的空间。
  • 我错过了 card_view:cardUseCompatPadding="true" 并且填充在我添加之前不起作用。添加此评论以防有人忽略此属性。
  • 我和@tronman 有同样的问题。添加 card_view:cardUseCompatPadding="true" 之后就可以了。
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="4dp"
    app:cardElevation="1dp"
    app:cardMaxElevation="1dp"
    app:cardUseCompatPadding="true" >  //this line is game changer

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 2020-10-04
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多