3. 幀布局 FrameLayout
幀布局的特點為從父容器的左上角開始繪制布局,是以所有位于幀布局之内的子控件或者容器之間可以互相覆寫。
1. 标簽 <FrameLayout />
2. 基本屬性
- id :表示目前布局或控件的唯一辨別,建構後自動在R.java檔案中生成一串辨別符,可作為查找和引用控件的參考。
- layout_width:表示目前布局的寬度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- layout_height :表示目前布局的高度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- background :表示目前布局的背景,可以用十六進制的RGB、ARGB 或 RRGGBB、AARRGGBB來表示,也可以調用 drawble 檔案夾當中的圖檔作為背景。
- padding : 表示目前布局内部空間的填充值,一般使用dp來作為衡量機關,線性布局從左上角開始,也可以選擇方向paddingTop,paddingLeft,paddingRight,paddingBottom。
- margin : 表示目前控件或布局距離周圍空間布局或父布局的距離,可選方向為margin_left,margin_top,margin_bottom,margin_right。
- forground:表示目前幀布局中控件的前景,可以是十六進制顔色或圖檔,永遠顯示在目前控件的最上層。
- forground_gravity:表示目前控件中前景的布局位置,取值可以是 top\right\bottom等。
- layout_gravity: 用來處理目前幀布局中子控件位于幀布局當中的位置,是以此屬性一定處于子控件的标簽中。
3. 代碼執行個體
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FrameLayout"
android:foreground="@drawable/ic_launcher_background"
android:foregroundGravity="center">
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#f00"
></TextView>
<TextView
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#ff453216"
></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f66565"
></TextView>
</FrameLayout>
4. 表格布局 TableLayout
表格布局的特點是控件會直接在布局中占用一行的大小,但和線性布局有差别,使用一個button控件在表格布局中和線上性水準布局中是不一樣的,屬性的寬高可設定為wrap_content。另外如果要多行顯示可以使用标簽<TableRow >并将每行中的控件放置其中。缺點是布局比較呆闆,适用于電腦類的界面。
1. 标簽:<TableLayout >
2. 基本屬性:
- id :表示目前布局或控件的唯一辨別,建構後自動在R.java檔案中生成一串辨別符,可作為查找和引用控件的參考。
- layout_width:表示目前布局的寬度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- layout_height :表示目前布局的高度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- background :表示目前布局的背景,可以用十六進制的RGB、ARGB 或 RRGGBB、AARRGGBB來表示,也可以調用 drawble 檔案夾當中的圖檔作為背景。
- padding : 表示目前布局内部空間的填充值,一般使用dp來作為衡量機關,線性布局從左上角開始,也可以選擇方向paddingTop,paddingLeft,paddingRight,paddingBottom。
- margin : 表示目前控件或布局距離周圍空間布局或父布局的距離,可選方向為margin_left,margin_top,margin_bottom,margin_right。
- layout_column/ layout_span :子控件跨列顯示以及跳列顯示.表格布局兩行以上才有效果
- stretch_column: 拉伸某列,取值為int,其中下标為0開始表示第1列,控件沒有占滿螢幕生效
- shrink_column: 收縮某列,取值同上,控件溢出螢幕生效
- collapse_column:隐藏某列,取值同上
3. 代碼執行個體:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TableLayout">
<TableRow>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
></Button>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_column="2"
></Button>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"></Button>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
></Button>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
></Button>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
android:layout_span="2"></Button>
</TableRow>
</TableLayout>
5. 網格布局 GridLayout
1. 标簽 <GridLayout />
2. 基本屬性:
控件屬性:
- id :表示目前布局或控件的唯一辨別,建構後自動在R.java檔案中生成一串辨別符,可作為查找和引用控件的參考。
- layout_width:表示目前布局的寬度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- layout_height :表示目前布局的高度,可以使用 match_parent 或 wrap_content 來表示目前的布局大小,或直接使用dp值固定大小。
- background :表示目前布局的背景,可以用十六進制的RGB、ARGB 或 RRGGBB、AARRGGBB來表示,也可以調用 drawble 檔案夾當中的圖檔作為背景。
- padding : 表示目前布局内部空間的填充值,一般使用dp來作為衡量機關,線性布局從左上角開始,也可以選擇方向paddingTop,paddingLeft,paddingRight,paddingBottom。
- margin : 表示目前控件或布局距離周圍空間布局或父布局的距離,可選方向為margin_left,margin_top,margin_bottom,margin_right。
- *orientation:*設定排列方式水準或垂直
- *layout_gravity:*設定對其方式,左右上下居中,如需要同時使用兩個方向可以使用
,例如
|
left|bottom
- *rowcount:*設定行數,指派為數字,例如
則有4行
rowcount = 4
- *columncount:*設定列數,用法同上
- *layout_row :*設定控件位于第幾行,指派為數字,例如
則控件位于第3行(從0開始計算)
layout_row = 2
- layout_column : 設定控件位于第幾列,用法同上
- layout_rowSpan: 設定控件縱向跨幾行,指派為數字,例如
則控件縱向跨4行
layout_rowSpan = 4
- layout_columnSpan: 設定控件橫向跨幾列,用法同上
3. 代碼執行個體:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GridLyout" android:orientation="horizontal" android:columnCount="4" android:rowCount="4"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" android:layout_row="4" android:layout_column="2" ></Button> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" android:layout_rowSpan="2"></Button> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button4"></Button> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button5" android:layout_columnSpan="2" ></Button> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button6" android:layout_rowSpan="2"></Button> <Button android:id="@+id/btn7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button7"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button8"></Button> </GridLayout>