android - 如何减少 RecyclerView 中 CardView 中卡片之间的空间?

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

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

enter image description here

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>

最佳答案

编辑这些属性

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>

关于android - 如何减少 RecyclerView 中 CardView 中卡片之间的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43299956/

相关文章:

java - 无法让位置管理器在 Fragment 内工作

android - viewpager 视差滚动和滚动时折叠工具栏

从电子邮件 Activity 返回后 Android 键盘仍然可见

jquery - XMLHttpRequest Origin null 不允许文件 :///to file:///(Serverless) 的 Access-Control-Allow-Origin

安卓电视 : VerticalGridFragment row items alignment

Android 应用程序名称不会显示在应用程序抽屉中

android - 单击网站上的 url 后打开应用程序 Skype、iMessage、WhatsApp 出现问题?

android - 更改 Android 的 xml 中的菜单导航图标颜色

Android 导航 View - 项目提升

android - Actionbarsherlock 开关操作栏覆盖模式以编程方式