代码如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:rowCount="6"
  6     android:columnCount="4">
  7     <TextView
  8         android:layout_width="match_parent"
  9         android:layout_height="200dp"
 10         android:layout_columnSpan="4"
 11         android:gravity="bottom|right"
 12         android:background="#ccc"/>
 13     <Button
 14         android:layout_width="0dp"
 15         android:layout_height="0dp"
 16         android:text="c"
 17         android:layout_columnWeight="1"
 18         android:layout_rowWeight="1"
 19         android:textSize="25sp"/>
 20     <Button
 21         android:layout_width="0dp"
 22         android:layout_height="0dp"
 23         android:text="( )"
 24         android:layout_columnWeight="1"
 25         android:layout_rowWeight="1"
 26         android:textSize="25sp"/>
 27     <Button
 28         android:layout_width="0dp"
 29         android:layout_height="0dp"
 30         android:text="÷"
 31         android:layout_columnWeight="1"
 32         android:layout_rowWeight="1"
 33         android:textSize="25sp"/>
 34     <Button
 35         android:layout_width="0dp"
 36         android:layout_height="0dp"
 37         android:text="×"
 38         android:layout_columnWeight="1"
 39         android:layout_rowWeight="1"
 40         android:textSize="25sp"/>
 41     <Button
 42         android:layout_width="0dp"
 43         android:layout_height="0dp"
 44         android:text="7"
 45         android:layout_columnWeight="1"
 46         android:layout_rowWeight="1"
 47         android:textSize="25sp"/>
 48     <Button
 49         android:layout_width="0dp"
 50         android:layout_height="0dp"
 51         android:text="8"
 52         android:layout_columnWeight="1"
 53         android:layout_rowWeight="1"
 54         android:textSize="25sp"/>
 55     <Button
 56         android:layout_width="0dp"
 57         android:layout_height="0dp"
 58         android:text="9"
 59         android:layout_columnWeight="1"
 60         android:layout_rowWeight="1"
 61         android:textSize="25sp"/>
 62     <Button
 63         android:layout_width="0dp"
 64         android:layout_height="0dp"
 65         android:text="-"
 66         android:layout_columnWeight="1"
 67         android:layout_rowWeight="1"
 68         android:textSize="25sp"/>
 69     <Button
 70         android:layout_width="0dp"
 71         android:layout_height="0dp"
 72         android:text="4"
 73         android:layout_columnWeight="1"
 74         android:layout_rowWeight="1"
 75         android:textSize="25sp"/>
 76     <Button
 77         android:layout_width="0dp"
 78         android:layout_height="0dp"
 79         android:text="5"
 80         android:layout_columnWeight="1"
 81         android:layout_rowWeight="1"
 82         android:textSize="25sp"/>
 83     <Button
 84         android:layout_width="0dp"
 85         android:layout_height="0dp"
 86         android:text="6"
 87         android:layout_columnWeight="1"
 88         android:layout_rowWeight="1"
 89         android:textSize="25sp"/>
 90     <Button
 91         android:layout_width="0dp"
 92         android:layout_height="0dp"
 93         android:text="+"
 94         android:layout_columnWeight="1"
 95         android:layout_rowWeight="1"
 96         android:textSize="25sp"/>
 97     <Button
 98         android:layout_width="0dp"
 99         android:layout_height="0dp"
100         android:text="1"
101         android:layout_columnWeight="1"
102         android:layout_rowWeight="1"
103         android:textSize="25sp"/>
104     <Button
105         android:layout_width="0dp"
106         android:layout_height="0dp"
107         android:text="2"
108         android:layout_columnWeight="1"
109         android:layout_rowWeight="1"
110         android:textSize="25sp"/>
111     <Button
112         android:layout_width="0dp"
113         android:layout_height="0dp"
114         android:text="3"
115         android:layout_columnWeight="1"
116         android:layout_rowWeight="1"
117         android:textSize="25sp"/>
118     <Button
119         android:layout_width="0dp"
120         android:layout_height="0dp"
121         android:text="="
122         android:layout_columnWeight="1"
123         android:layout_rowSpan="2"
124         android:layout_rowWeight="1"
125         android:textSize="25sp"
126         android:layout_gravity="fill"/>
127     <Button
128         android:layout_width="0dp"
129         android:layout_height="0dp"
130         android:text="0"
131         android:layout_columnWeight="1"
132         android:layout_columnSpan="2"
133         android:layout_rowWeight="1"
134         android:textSize="25sp"/>
135     <Button
136         android:layout_width="0dp"
137         android:layout_height="0dp"
138         android:text="."
139         android:layout_columnWeight="1"
140         android:layout_rowWeight="1"
141         android:textSize="25sp"/>
142 </GridLayout>

效果如图:

网格布局能够实现纵向和竖向合并