2016-01-21 57 views
1

我試圖在Android中創建一個靜態網格式佈局,因此我使用的是GridLayout而不是GridView試圖用不同大小的元素創建GridLayout的問題

這是我想要達到

here使用代碼爲基地的佈局。對於在網格內的每個元素上指定layout_{weight, margin}也有點困惑,因爲我的理解是rowSpancolSpan參數應該處理這個問題。這不正確嗎?

任何指針?

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/GridLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="2" 
    tools:context=".MainActivity" > 

    <android.support.v7.widget.CardView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/card_view" 
     android:layout_column="0" 
     android:layout_gravity="fill" 
     android:layout_row="0" 
     android:layout_columnSpan="1"> 
     <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/button3" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/card_view1" 
     android:layout_column="0" 
     android:layout_gravity="fill" 
     android:layout_row="1" 
     android:layout_columnSpan="1"> 
     <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/button1" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/card_view3" 
     android:layout_column="1" 
     android:layout_gravity="fill" 
     android:layout_row="1"> 
     <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/button2" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

</GridLayout> 
+0

檢查[this](http://stackoverflow.com/questions/27718928/android-gridlayout-api-21/27719757#27719757) – Elltz

回答

2

你是如此接近,試試這個來代替:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/GridLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="2" 
    tools:context=".MainActivity" > 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_column="0" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" 
     android:layout_columnSpan="2"> 
     <Button 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/button3" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view1" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_columnSpan="1" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/button1" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view3" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/button2" 
      android:text="Button" /> 
    </android.support.v7.widget.CardView> 

</GridLayout> 

您需要使用layout_gravity="fill_horizontal"的第一個項目,撐整行。第二和第三項與layout_columnWeight一起玩。

+0

不完全是我指定的,似乎有問題的rowSpan的第一個元素,它被忽略[這裏](http://i.imgur.com/kQ7YJqs.png) – Sebastialonso

+0

你能澄清更多?第一個元素需要2列,是不是你想要的? – Rami

+1

對不起,我混了!這是完全正確的,謝謝! – Sebastialonso