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」'確保在棒棒糖上使用相同的算術和分配的空間。 –

相關問題