2017-08-10 53 views
0

我一直在我的卡片視圖中遇到問題,android studio在它們之間放置太多空間。 Here is an example screenshot我從模擬器中拿走了。因爲在這裏我的佈局吹氣是我使用的代碼:CardViews之間的空間太大

@Override 
public DriveStatsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) 
{ 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_layout, viewGroup, false); 
    DriveStatsViewHolder dsvh = new DriveStatsViewHolder(v); 
    return dsvh; 
} 

下面是我cardview_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
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="match_parent"> 

<android.support.v7.widget.CardView 
    android:id="@+id/cv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="1.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.0"> 

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

     <TextView 
      android:id="@+id/FinishTime" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/Drive_Date" 
      android:layout_toStartOf="@+id/Total_Drive_Time" 
      android:textAlignment="textEnd" 
      android:textSize="18sp" 
      tools:text="Finish Time"/> 

     <TextView 
      android:id="@+id/Total_Drive_Time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentTop="true" 
      android:textAlignment="viewStart" 
      android:textSize="18sp" 
      tools:text="Total Drive Time"/> 

     <TextView 
      android:id="@+id/Drive_Date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:textSize="24sp" 
      tools:text="Default Text"/> 

     <TextView 
      android:id="@+id/Start_Time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/Drive_Date" 
      android:textSize="18sp" 
      tools:text="Start Time"/> 

    </RelativeLayout> 

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

我非常新到Android工作室所以任何幫助,將不勝感激。

+0

對不起,我應該指定上面的xml是cardview_layout.xml –

+4

只需刪除外部的RelativeLayout。或者,只要使RelativeLayout'wrap_content'的高度爲 –

+1

@DanielNugent我認爲是一樣的:),但'android:layout_height =「wrap_content」' –

回答

1

您的外部RelativeLayout的高度爲match_parent,因此您列表中的每個項目都與屏幕一樣高。然而,CardView本身的高度僅爲wrap_content,因此大部分空間都將保留爲空。