3

我正在为我的应用程序的主屏幕进行布局。我正在使用带有网格布局和CardView的RecyclerView。我试图减少每张卡之间的空间。截图如下(打开布局大纲)以及我的布局文件。我怎样才能让每张卡在垂直方向和水平方向上彼此接近?我一直在利用很多利润,我似乎无法找到合适的保证金或填充值来做到这一点。谢谢你的帮助!如何减少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> 
+0

您是否尝试将'CardView'的宽度更改为'wrao_content'? – azizbekian

+0

我没有尝试它,直到你建议它,但它没有什么区别。另外,我希望每张卡都具有相同的尺寸,所以我不认为我会想'wrap_content'。不过谢谢。 – intA

回答

5

编辑这些属性

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

所以完整的代码将b e

<?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> 
+0

谢谢!这工作。有点奇怪的是,高程控制卡之间的间距。我认为这与页面上感知的高程有关,以影响阴影和偏移量等事情。 – intA

+0

@intA在Lollipop下不支持海拔。为compat阴影添加额外的空间。更大的潜在影子(maxCardElevation)意味着分配的空间更多。它的水平方向为1 * maxCardElevation,垂直方向为1.5 * maxCardElevation。默认卡片高度是2dp。 –

+0

@intA'app:cardUseCompatPadding =“true”'确保在棒棒糖上使用相同的算术和分配的空间。 –