2015-07-11 64 views
7

我有多個CardView的在XML定義爲:如何製作可變高度CardViews的網格佈局?

<android.support.v7.widget.CardView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    card_view:cardPreventCornerOverlap="false" 
    card_view:cardCornerRadius="4dp"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:textSize="@dimen/abc_text_size_title_material" 
      android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

對於電話,我已包裹所述多個CardView的在垂直LinearLayout

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

對於片劑,我想改爲使用網格佈局,類似於Material Design specs中使用的設計,請參閱圖像here

用於製作網格佈局的最佳ViewGroup是什麼?這些卡的高度可變,並且具有靜態內容。

GridLayout不能使用,因爲卡的高度是可變的。

回答

12

我在某段時間面臨同樣的問題。是的,你不能使用GridLayout作爲你的卡的大小可變。所以你在找什麼StaggeredGridView它允許它的項目有不同的大小。

可以執行下列操作的: -

1)您可以使用thisthis庫。它們使用起來非常簡單。我已經用完了,我可以向你保證,他們完美地完成了他們的工作。

2)您可以嘗試Google提供的RecyclerView.StaggeredGridLayoutManager

樣品代碼看起來有些事情就是這樣,

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); 
    recyclerView.setAdapter(adapter); 

希望它可以幫助你。

+0

第二種選擇是最好的我猜.. – Hardy

1

對於平板電腦,您可以使用2個LinearLayouts。