Android 布局

  1 activity布局初步 
  2 LinearLayout 线性布局 就是以一条线的形式就行布局 可以分为直线型和垂直型 
  3   
  4 <!-- 
  5         android:id  —— 为控件指定相应的ID 
  6         android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 
  7         android:grivity —— 指定控件的基本位置,比如说居中,居右等位置 
  8         android:textSize —— 指定控件当中字体的大小 
  9         android:background —— 指定该控件所使用的背景色,RGB命名法  
 10         android:width —— 指定控件的宽度 
 11         android:height —— 指定控件的高度 
 12         android:padding* —— 指定控件的内边距,也就是说控件当中的内容 
 13         android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示 
 14 -->
 15     <TextView
 16         android:id="@+id/firstText"
 17         android:text="第一行"
 18         android:gravity="center_vertical"
 19         android:textSize="35pt"
 20         android:background="#aa0000"
 21         android:layout_width="fill_parent"
 22         android:layout_height="wrap_content"
 23         android:paddingLeft="10dip"
 24         android:paddingTop="20dip"
 25         android:paddingRight="30dip"
 26         android:paddingBottom="40dip"
 27         android:layout_weight="1"
 28         android:singleLine="true"/> 
 29     <TextView
 30 --> 
 31   
 32 LinearLayout嵌套LinearLayout的布局方式 
 33 <?xml version="1.0" encoding="utf-8"?> 
 34 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 35     android:orientation="vertical"
 36     android:layout_width="fill_parent"
 37     android:layout_height="fill_parent"
 38     > 
 39     <LinearLayout 
 40         android:orientation="horizontal"
 41         android:layout_width="fill_parent"
 42         android:layout_height="fill_parent"
 43         android:layout_weight="1"> 
 44         <TextView
 45           android:text="red"
 46           android:gravity="center_horizontal"
 47           android:background="#aa0000"
 48           android:layout_width="wrap_content"
 49           android:layout_height="fill_parent"
 50           android:layout_weight="1"/> 
 51       <TextView
 52           android:text="green"
 53           android:gravity="center_horizontal"
 54           android:background="#00aa00"
 55           android:layout_width="wrap_content"
 56           android:layout_height="fill_parent"
 57           android:layout_weight="1"/> 
 58       <TextView
 59           android:text="blue"
 60           android:gravity="center_horizontal"
 61           android:background="#0000aa"
 62           android:layout_width="wrap_content"
 63           android:layout_height="fill_parent"
 64           android:layout_weight="1"/> 
 65       <TextView
 66           android:text="yellow"
 67           android:gravity="center_horizontal"
 68           android:background="#aaaa00"
 69           android:layout_width="wrap_content"
 70           android:layout_height="fill_parent"
 71           android:layout_weight="1"/> 
 72     </LinearLayout> 
 73       
 74       
 75     <LinearLayout 
 76         android:orientation="vertical"
 77         android:layout_width="fill_parent"
 78         android:layout_height="fill_parent"
 79         android:layout_weight="1"> 
 80     <TextView
 81         android:text="row one"
 82         android:textSize="15pt"
 83         android:layout_width="fill_parent"
 84         android:layout_height="wrap_content"
 85         android:layout_weight="1"/> 
 86     <TextView
 87         android:text="row two"
 88         android:textSize="15pt"
 89         android:layout_width="fill_parent"
 90         android:layout_height="wrap_content"
 91         android:layout_weight="1"/> 
 92     <TextView
 93         android:text="row three"
 94         android:textSize="15pt"
 95         android:layout_width="fill_parent"
 96         android:layout_height="wrap_content"
 97         android:layout_weight="1"/> 
 98     <TextView
 99         android:text="row four"
100         android:textSize="15pt"
101         android:layout_width="fill_parent"
102         android:layout_height="wrap_content"
103         android:layout_weight="1"/> 
104     </LinearLayout> 
105 </LinearLayout> 
106   
107 ------------------------------------------------------------------------------------------------ 
108   
109 TableLayout表格布局 就是以表格的形式就行布局 
110 <?xml version="1.0" encoding="utf-8"?> 
111 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
112     android:layout_width="fill_parent"
113     android:layout_height="fill_parent"
114     android:stretchColumns="0"> 
115     <TableRow> 
116         <TextView
117             android:text="@string/row1_column1"
118             android:background="#aa0000"
119             android:padding="3dip" /> 
120         <TextView
121             android:text="@string/row1_column1"
122             android:padding="3dip"
123             android:gravity="center_horizontal"
124             android:background="#00aa00"
125             ></TextView> 
126         <TextView
127             android:text="@string/row1_column2"
128             android:gravity="right"
129             android:background="#0000aa"
130             android:padding="3dip" /> 
131     </TableRow> 
132   
133     <TableRow> 
134         <TextView
135             android:text="@string/row2_column1"
136             android:padding="3dip" /> 
137         <TextView
138             android:text="@string/row2_column2"
139             android:gravity="right"
140             android:padding="3dip" /> 
141     </TableRow> 
142 </TableLayout> 
143   
144   
145 LinearLayout和LinearLayout的布局形式 
146 android:layout_weight="1",android:layout_weight="1"就是把这个layout分成2份 1表示占其中的一份 
147 <?xml version="1.0" encoding="utf-8"?> 
148 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
149     android:orientation="vertical" android:layout_width="fill_parent"
150     android:layout_height="fill_parent"> 
151     <LinearLayout 
152         android:orientation="horizontal"
153         android:layout_width="fill_parent" 
154         android:layout_height="fill_parent"
155         android:layout_weight="1"> 
156         <TextView 
157             android:text="red" 
158             android:gravity="center_horizontal"
159             android:background="#aa0000" 
160             android:layout_width="wrap_content"
161             android:layout_height="fill_parent" 
162             android:layout_weight="1" /> 
163         <TextView 
164             android:text="green" 
165             android:gravity="center_horizontal"
166             android:background="#00aa00" 
167             android:layout_width="wrap_content"
168             android:layout_height="fill_parent" 
169             android:layout_weight="1" /> 
170         <TextView 
171             android:text="blue" 
172             android:gravity="center_horizontal"
173             android:background="#0000aa" 
174             android:layout_width="wrap_content"
175             android:layout_height="fill_parent" 
176             android:layout_weight="1" /> 
177         <TextView 
178             android:text="yellow" 
179             android:gravity="center_horizontal"
180             android:background="#aaaa00" 
181             android:layout_width="wrap_content"
182             android:layout_height="fill_parent" 
183             android:layout_weight="1" /> 
184     </LinearLayout> 
185   
186   
187     <LinearLayout 
188         android:orientation="horizontal"
189         android:layout_width="fill_parent" 
190         android:layout_height="fill_parent"
191         android:layout_weight="1"> 
192         <TableLayout 
193             xmlns:android="http://schemas.android.com/apk/res/android"
194             android:layout_width="fill_parent" 
195             android:layout_height="fill_parent"
196             android:stretchColumns="0"> 
197             <TableRow> 
198                 <TextView 
199                     android:text="@string/row1_column1"
200                     android:padding="3dip" /> 
201                 <TextView 
202                     android:text="@string/row1_column1"
203                     android:padding="3dip" 
204                     android:gravity="center_horizontal"> 
205                     </TextView> 
206                 <TextView 
207                     android:text="@string/row1_column2"
208                     android:gravity="right" 
209                     android:padding="3dip" /> 
210             </TableRow> 
211   
212             <TableRow> 
213                 <TextView 
214                     android:text="@string/row2_column1"
215                     android:padding="3dip" /> 
216                 <TextView 
217                     android:text="@string/row2_column2"
218                     android:gravity="right" 
219                     android:padding="3dip" /> 
220             </TableRow> 
221         </TableLayout> 
222     </LinearLayout> 
223 </LinearLayout> 
224   
225   
226 -------------------------------------------------------------------------------- 
227 RelativeLayout相对布局形式 以控件的方位进行布局 
228 <?xml version="1.0" encoding="utf-8"?> 
229     <!-- 
230         android:layout_above 将该控件的底部至于给定ID的控件之上 
231         android:layout_below 将该控件的顶部至于给定ID的控件之下 
232         android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐 
233         android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐 
234   
235         android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐 
236         android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘 
237         android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐 
238         android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐 
239         android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐 
240   
241   
242         android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐 
243         android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐 
244         android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐 
245         android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐 
246   
247         android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央 
248         android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央 
249         android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央 
250     -->
251 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
252                 android:layout_width="fill_parent"
253                 android:layout_height="wrap_content"
254                 android:padding="10px" > 
255   
256     <TextView android:id="@+id/label" 
257               android:layout_width="fill_parent" 
258               android:layout_height="wrap_content" 
259               android:text="Type here:" /> 
260   
261     <EditText android:id="@+id/entry" 
262               android:layout_width="fill_parent" 
263               android:layout_height="wrap_content" 
264               android:background="@android:drawable/editbox_background"
265               android:layout_below="@id/label" /> 
266     
267     <Button android:id="@+id/ok" 
268             android:layout_width="wrap_content" 
269             android:layout_height="wrap_content" 
270             android:layout_below="@id/entry"
271             android:layout_alignParentRight="true"
272             android:layout_marginLeft="10px"
273             android:text="OK" /> 
274   
275     <Button android:layout_width="wrap_content" 
276             android:layout_height="wrap_content"
277             android:layout_toLeftOf="@id/ok"
278             android:layout_alignTop="@id/ok"
279             android:text="Cancel" /> 
280 </RelativeLayout> 
原文地址:https://www.cnblogs.com/dchly/p/2802610.html