2016-01-21 73 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