Android中布局管理器本身也是一个界面控件,所有的布局管理器都是ViewGroup类的子类,都可以当作容器类来使用。因此,可以在一个布局管理器中嵌套其他布局管理器。Android中常用的布局管理器主要有:线性布局,表格布局,相对布局,层布局以及网格布局。
- 线性布局:它用LinearLayout类表示,所谓的线性是指所有的控件沿着同一个方向进行排列,在Android中,只提供了水平和垂直两种方向,可通过android:orientation属性进行设置,默认为水平。
常见属性:
android:gravity:设置线性布局内控件的对齐方式。例如:bottom|center_horizental代表出现在屏幕底部,而且水平居中。
android:orientation:设置线性布局内控件的排列方向,只有vertical或horizontal两种。
android:layout_weight:设置线性布局内控件的宽度或高度所占剩余空间的权重。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="上"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="左"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:text="中"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="右"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="下"
/>
</LinearLayout>
- 表格布局:它用TableLayout类表示,但并未提供相关属性来设置包含几行几列,而是通过在TableLayout中添加TableRow来添加行。
常见属性:
android:collapseColumns:隐藏指定列,其值为列所在的序号,从0开始。
android:layout_column:指定控件在TableRow中所处的列。
android:layout_span:指定控件所跨越的列数。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="2"
>
<TableRow>
<Button android:text="按钮一"/>
<Button android:text="按钮三"
android:layout_column="2"/>
</TableRow>
<TableRow>
<Button android:text="按钮四"/>
<Button android:text="按钮五"/>
<Button android:text="按钮六"/>
</TableRow>
<TableRow>
<Button android:text="按钮七"/>
<Button android:text="按钮八" android:layout_span="2"/>
</TableRow>
</TableLayout>
其他控件下次分享!!!