Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 從零入門(3):視圖組件之布局管理器全解

編輯:關於 Android 程式設計

為什麼需要布局管理器

Activity 透過 setContentView(xml resId) 把自己的顯示介面綁定上去。但介面上的控件品種繁多——按鈕、文本框、編輯框、圖片等等,如果每個的位置都靠代碼硬算,維護起來會非常痛苦。為了解決這個問題,Android 提供了布局管理器:讓圖形介面與 Java 代碼彼此分離,代碼只負責數據處理和行為控制,界面排布交給 XML。這種思路和 Java 裡的 MVC 頗為相似——布局管理器扮演 View,Java 代碼扮演 Control,數據存儲則是 Model。

Android 常見的布局管理器有五種:線性布局(LinearLayout)、相對布局(RelativeLayout)、網格布局(GridLayout)、幀布局(FrameLayout)以及絕對布局(AbsoluteLayout)。下面逐一說明。

1. 線性布局(LinearLayout)

官方文檔的說法是:把子元素排列在單一的行或列上。方向可以透過 setOrientation() 設定為水平或垂直,排列方式則可透過 LayoutParams 的 setGravity() 指定,也可以給特定子元素設定權重來分配剩餘空間,默認方向為水平。

這裡有一點非常重要:LinearLayout 中的子元素沿排列方向一旦超出螢幕寬度,多餘的部分是不會顯示出來的。

官方文檔的屬性一覽:

逐項說明幾個常用屬性:

baselineAligned 設為 false 時,會阻止布局與其子元素的基線對齊。當子元素使用了不同的權重值時,這個屬性特別有用,默認值為 true,取值必須是布林值。

dividerVertical 用於設定垂直布局時兩個按鈕之間的分隔線。

gravity 決定布局內組件的對齊方式,支援 top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal 等取值,也可以複合指定,例如 left | center_vertical 表示靠左且垂直居中。

measureWithLargestChild 設為 true 時,所有帶權重的子元素都會採用最大子元素的最小尺寸。

orientation 決定排列方向,可選 horizontal(水平)或 vertical(垂直),默認是垂直。

權重的用法舉個例子:android:weightSum="5" 表示把整個 LinearLayout 分成 5 等份;內部控件寫 android:layout_weight="1",就代表它佔據其中 1/5。

幾個屬性組合下來的實際效果:

看完這幾張對照圖,各屬性的作用就很直觀了。

2. 相對布局(RelativeLayout)

其餘幾種布局的屬性,建議直接照著上面的方法去官網查閱文檔,這裡就用更精簡的方式帶過。

RelativeLayout 中的子組件位置,總是相對於兄弟組件或父容器來確定的,因此被稱為相對布局。要注意引用順序:如果 B 的位置由 A 決定,就必須先定義並確定 B,才能定義 A。

容器級屬性:

android:gravity → setGravity(int):設定容器內各子組件的對齊方式。

android:ignoreGravity → setIgnoreGravity(int):指定哪個組件不受 gravity 影響。

RelativeLayout 另外提供內部類 RelativeLayout.LayoutParams,用大量 XML 屬性精細控制子組件的分布。以下屬性的取值只能是 true 或 false:

android:layout_centerHorizontal:是否水平居中。

android:layout_centerVertical:是否垂直居中。

android:layout_centerInParent:是否位於容器正中。

android:layout_alignParentBottom / Left / Right / Top:是否與容器底、左、右、頂對齊。

以下屬性的取值必須是其他 UI 組件的 id:

android:layout_toRightOf / layout_toLeftOf:位於指定 id 組件的右側/左側。

android:layout_above / layout_below:位於指定 id 組件的上方/下方。

android:layout_alignTop / alignBottom / alignLeft / alignRight:與指定 id 組件的上、下、左、右邊界對齊。

用一個雪花散開的效果來演示 RelativeLayout:

<relativelayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">
    <!-- 位於父容器中央 -->
    <textview android:id="@+id/xuehua_1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 位於 view1 上方 -->
    <textview android:id="@+id/xuehua_2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_above="@id/xuehua_1" android:layout_alignLeft="@id/xuehua_1"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 位於 view1 下方 -->
    <textview android:id="@+id/xuehua_3"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignLeft="@id/xuehua_1" android:layout_below="@id/xuehua_1"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 位於 view1 左方 -->
    <textview android:id="@+id/xuehua_4"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignTop="@id/xuehua_1" android:layout_toLeftOf="@id/xuehua_1"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 位於 view1 右方 -->
    <textview android:id="@+id/xuehua_5"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignTop="@id/xuehua_1" android:layout_toRightOf="@id/xuehua_1"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 父容器上方中央 -->
    <textview android:id="@+id/xuehua_6"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 父容器左邊中央 -->
    <textview android:id="@+id/xuehua_7"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentRight="true" android:layout_centerVertical="true"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 父容器右邊中央 -->
    <textview android:id="@+id/xuehua_8"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" android:layout_centerVertical="true"
        android:background="@drawable/iconfont_xuehua" />
    <!-- 父容器下方居中 -->
    <textview android:id="@+id/xuehua_9"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"
        android:background="@drawable/iconfont_xuehua" />
</relativelayout>

效果圖:

3. 網格布局(GridLayout)

GridLayout 是 Android 4.0 之後新增的布局管理器。顧名思義,它會把整個容器劃分成 rows × columns 個格子,每格放一個組件,同時還可以指定某個組件橫向跨幾列、縱向跨幾行。

android:alignmentMode → setAlignmentMode(int):設定對齊模式。

android:columnCount → setColumnCount(int):設定列數。

android:columnOrderPreserved → setColumnOrderPreserved(boolean):是否保留列序號。

android:rowCount → setRowCount(int):設定行數。

android:rowOrderPreserved → setRowOrderPreserved(boolean):是否保留行序號。

android:useDefaultMargins → setDefaultMargins(boolean):是否使用默認邊距。

控制子組件分布的內部類是 GridLayout.LayoutParams:

android:layout_column:該子組件位於第幾列。

android:layout_columnSpan:橫向跨幾列。

android:layout_gravity → setGravity(int):以何種方式佔據格子空間。

android:layout_row:該子組件位於第幾行。

android:layout_rowSpan:縱向跨幾行。

以計算器為例:

<gridlayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:background="#000" android:columnCount="4" android:rowCount="6"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <!-- 橫跨 4 列的顯示框 -->
    <textview android:background="#EEEEEE" android:layout_columnSpan="4"
        android:layout_gravity="right" android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" android:padding="5dp"
        android:singleLine="true" android:text="0" android:textColor="#000" android:textSize="50sp">
    <!-- 橫跨 4 列的清除鍵 -->
    <button android:layout_columnSpan="4" android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="清除" android:textSize="22sp" />
    <button android:text="7" android:textSize="20sp" />
    <button android:text="8" android:textSize="20sp" />
    <button android:text="9" android:textSize="20sp" />
    <button android:layout_gravity="fill" android:text="/" android:textSize="20sp" />
    <button android:text="4" android:textSize="20sp" />
    <button android:text="5" android:textSize="20sp" />
    <button android:text="6" android:textSize="20sp" />
    <button android:layout_gravity="fill" android:text="*" android:textSize="20sp" />
    <button android:text="1" android:textSize="20sp" />
    <button android:text="2" android:textSize="20sp" />
    <button android:text="3" android:textSize="20sp" />
    <button android:layout_gravity="fill" android:text="-" android:textSize="20sp" />
    <button android:text="." android:textSize="20sp" />
    <button android:text="0" android:textSize="20sp" />
    <button android:text="=" android:textSize="20sp" />
    <button android:layout_gravity="fill_horizontal" android:text="+" android:textSize="20sp" />
    </textview>
</gridlayout>

效果圖:

4. 幀布局(FrameLayout)

幀布局會為加入其中的每個組件創建一個空白區域,稱為一幀。所有子組件都佔據同一幀的位置,再依 gravity 屬性各自對齊——理解方式就像撲克牌,一張一張疊在一起,後加入的蓋在先加入的上面。

android:foreground → setForeground(Drawable):設定前景圖像。

android:foregroundGravity → setForegroundGravity(int):定義前景圖像的绘制位置。

<framelayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <textview android:background="#f00" android:layout_gravity="center"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="300dp">
    <textview android:background="#0f0" android:layout_gravity="center"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="200dp">
    <textview android:background="#00f" android:layout_gravity="center"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="100dp" />
    </textview>
</framelayout>

效果圖:

5. 絕對布局(AbsoluteLayout)

絕對布局靠直接指定 X、Y 座標來擺放組件,布局管理器完全不再插手子組件的位置與大小。這種做法並不推薦:市面上 Android 設備的螢幕尺寸與解析度差異極大,寫死座標不可能在所有螢幕上都呈現一致的效果。因此 AbsoluteLayout 已經不適合再用於實際項目。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved