案例:TableLayout表格布局——迷你计算器

  计算器可以常用线性布局(LinearLayout)和表格布局(tableLayout)、Gridlayout

  今天我用的是表格布局

  效果如下:

    

代码如下:

  

  1 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:stretchColumns="*"
  6     tools:context="com.calc.minicalculator.MainActivity" >
  7 
  8 
  9         
 10          <TextView
 11          android:layout_weight="3"//权重
 12         android:id="@+id/textView1"
 13         android:layout_width="match_parent"
 14         android:layout_height="wrap_content"
 15         android:layout_gravity="center_horizontal|top"//设置控件里文字格式
 16         android:gravity="right|center_vertical"//设置控件摆放位置
 17         android:text="@string/text_result"
 18         android:textColor="@color/darkblue"
 19         android:textSize="40dp"
 20         android:textStyle="bold" />
 21 
 22          
 23          <TableRow
 24              android:layout_weight="1"
 25              android:id="@+id/tableRow1"
 26              android:layout_width="wrap_content"
 27              android:layout_height="wrap_content" >
 28 
 29              <Button
 30                  android:id="@+id/button1"
 31                  android:layout_width="wrap_content"
 32                  android:layout_height="match_parent"
 33                  android:text="@string/clear" />
 34 
 35              <Button
 36                  android:id="@+id/button2"
 37                  android:layout_width="wrap_content"
 38                  android:layout_height="match_parent"
 39                  android:text="@string/btn_back" />
 40 
 41              <Button
 42                  android:id="@+id/button3"
 43                  android:layout_width="wrap_content"
 44                  android:layout_height="match_parent"
 45                  android:text="@string/btn_add" />
 46 
 47              <Button
 48                  android:id="@+id/button4"
 49                  android:layout_width="wrap_content"
 50                  android:layout_height="match_parent"
 51                  android:text="@string/btn_sub" />
 52 
 53          </TableRow>
 54 
 55          <TableRow
 56              android:layout_weight="1"
 57              android:id="@+id/tableRow2"
 58              android:layout_width="wrap_content"
 59              android:layout_height="wrap_content" >
 60 
 61              <Button
 62                  android:id="@+id/button5"
 63                  android:layout_width="wrap_content"
 64                  android:layout_height="match_parent"
 65                  android:text="@string/btn_7" />
 66 
 67              <Button
 68                  android:id="@+id/button6"
 69                  android:layout_width="wrap_content"
 70                  android:layout_height="match_parent"
 71                  android:text="@string/btn_8" />
 72 
 73              <Button
 74                  android:id="@+id/button7"
 75                  android:layout_width="wrap_content"
 76                  android:layout_height="match_parent"
 77                  android:text="@string/btn_9" />
 78 
 79              <Button
 80                  android:id="@+id/button8"
 81                  android:layout_width="wrap_content"
 82                  android:layout_height="match_parent"
 83                  android:text="@string/btn_mul" />
 84 
 85          </TableRow>
 86 
 87          <TableRow
 88              android:layout_weight="1"
 89              android:id="@+id/tableRow3"
 90              android:layout_width="wrap_content"
 91              android:layout_height="wrap_content" >
 92 
 93              <Button
 94                  android:id="@+id/button9"
 95                  android:layout_width="wrap_content"
 96                  android:layout_height="match_parent"
 97                  android:text="@string/btn_6" />
 98 
 99              <Button
100                  android:id="@+id/button10"
101                  android:layout_width="wrap_content"
102                  android:layout_height="match_parent"
103                  android:text="@string/btn_5" />
104 
105              <Button
106                  android:id="@+id/button11"
107                  android:layout_width="wrap_content"
108                  android:layout_height="match_parent"
109                  android:text="@string/btn_4" />
110 
111              <Button
112                  android:id="@+id/button12"
113                  android:layout_width="wrap_content"
114                  android:layout_height="match_parent"
115                  android:text="@string/btn_divide" />
116 
117          </TableRow>
118 
119          <TableRow
120              android:layout_weight="1"
121              android:id="@+id/tableRow4"
122              android:layout_width="wrap_content"
123              android:layout_height="wrap_content" >
124 
125              <Button
126                  android:id="@+id/button13"
127                  android:layout_width="wrap_content"
128                  android:layout_height="match_parent"
129                  android:text="@string/btn_1" />
130 
131              <Button
132                  android:id="@+id/button14"
133                  android:layout_width="wrap_content"
134                  android:layout_height="match_parent"
135                  android:text="@string/btn_2" />
136 
137              <Button
138                  android:id="@+id/button15"
139                  android:layout_width="wrap_content"
140                  android:layout_height="match_parent"
141                  android:text="@string/btn_3" />
142 
143              <Button
144                  android:id="@+id/button19"
145                  android:layout_width="wrap_content"
146                  android:layout_height="match_parent"
147                  android:text="@string/btn_point" />
148 
149          </TableRow>
150 
151          <TableRow
152              android:layout_weight="1"
153              android:id="@+id/tableRow5"
154              android:layout_width="wrap_content"
155              android:layout_height="wrap_content" 
156           >
157 
158              <Button
159                  android:id="@+id/button17"
160                  android:layout_width="wrap_content"
161                  android:layout_height="match_parent"
162                  android:text="@string/btn_0" 
163                 android:layout_span="2"
164                  />
165 
166              <Button
167                  android:id="@+id/button16"
168                  android:layout_width="wrap_content"
169                  android:layout_height="match_parent"
170                  android:text="@string/btn_den" 
171                   android:layout_span="2"/>
172 
173          </TableRow>
174 
175 </TableLayout>

  

原文地址:https://www.cnblogs.com/lgk1002/p/6094928.html