Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android UI 布局入門:View、ViewGroup 與常見配置類型

編輯:進階開發

使用者介面最基本的建材是 View。一個 View 物件佔據螢幕上的一塊矩形區域,負責把自己畫出來並處理觸發於其上的事件。Button、TextView 這些常見元件,全都是 View 的子類別。

ViewGroup 則是 View 的子類別,本身不顯示任何內容,作用是作為不可見的容器,用來裝載其他 View 或其他 ViewGroup,並決定它們的配置方式。

而在第三層,不同的「布局」正是 ViewGroup 的各個子類別。一個典型的 Android 介面就是由這些布局定義出來的,建立方式有兩種:在執行期用 View/ViewGroup 物件組出可視結構,或者用 XML 檔(例如專案 res/layout 目錄下的 main_layout.xml)宣告式地描述。本文聚焦於後者,因為 XML 定義介面更直覺、也更容易維護。

布局檔案裡可以放任何類型的元件——按鈕、標籤、文字框等等。以下是一個最簡單的 LinearLayout 例子:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a Button" /> <!-- More GUI components go here --> </LinearLayout> 

布局寫好之後,就能在 Activity 的 onCreate() 回呼中把它載入畫面:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 

Android 提供的布局類型

Android 內建了多種布局,幾乎涵蓋所有常見的畫面結構需求,各有不同的排列邏輯與外觀風格:

布局說明
Linear Layout把子元件沿單一方向排列,可選垂直或水平。
Relative Layout依相對位置安排子元件,可指定相對於父容器或其他兄弟元件。
Table Layout把子元件劃分成列與欄的表格結構。
Absolute Layout以絕對座標指定每個子元件的精確位置。
Frame Layout螢幕上的一個佔位符,通常用來容納單一檢視。
List View顯示可捲動的項目清單。
Grid View以二維網格呈現可捲動的項目群組。

布局屬性

每種布局都有一組屬性用來描述視覺外觀。有些屬性在各種布局之間通用,以下是最常用的一批:

屬性說明
android:id為該元件指定唯一識別碼
android:layout_width布局寬度
android:layout_height布局高度
android:layout_marginTop頂側外距
android:layout_marginBottom底側外距
android:layout_marginLeft左側外距
android:layout_marginRight右側外距
android:layout_gravity指定子元件在容器中的位置
android:layout_weight指定配置多餘空間時各元件佔的比例
android:layout_x配置的 x 座標
android:layout_y配置的 y 座標
android:paddingLeft左側內距
android:paddingRight右側內距
android:paddingTop頂部內距
android:paddingBottom底部內距

寬度與高度可以用幾種單位指定:dp(密度獨立像素)、sp(比例獨立像素)、pt(點,1/72 吋)、px(像素)、mm(毫米)與 in(英吋)。實務上為了讓介面在不同解析度下都能正常呈現,dp 與 sp 是最常用的選擇。

雖然可以寫死精確數值,但更常見的做法是使用這兩個常量:

  • android:layout_width=wrap_content:告訴系統「我只要剛好容納內容的大小」。
  • android:layout_width=fill_parent:告訴系統「請和父容器一樣大」。

Gravity 屬性在決定元件位置時扮演關鍵角色。它可以接受一個或多個(以「|」分隔)下列常量:

常量值說明
top0x30推向容器頂端,不改變尺寸
bottom0x50推向容器底端,不改變尺寸
left0x03推向容器左側,不改變尺寸
right0x05推向容器右側,不改變尺寸
center_vertical0x10置於容器垂直中心,不改變尺寸
fill_vertical0x70必要時拉長垂直尺寸以填滿容器
center_horizontal0x01置於容器水平中心,不改變尺寸
fill_horizontal0x07必要時拉長水平尺寸以填滿容器
center0x11置於容器水平與垂直雙軸中心,不改變尺寸
fill0x77必要時同時填滿水平與垂直方向
clip_vertical0x80讓子元件頂部或底部邊緣裁切貼齊容器邊界,究竟裁哪一側取決於搭配的垂直重力
clip_horizontal0x08讓子元件左側或右側邊緣裁切貼齊容器邊界,裁哪一側取決於搭配的水平重力
start0x00800003推向容器起點,不改變尺寸
end0x00800005推向容器終點,不改變尺寸

視圖識別碼

每個 View 都可以配上一個唯一的 ID,供後續在程式碼中辨識使用。在 XML 標籤中的寫法如下:

 android:id="@+id/my_button" 

這裡的 @+ 兩個符號各有意義:

  • @ 開頭表示後面的字串要交給 XML 解析器處理,並被視為一個資源 ID 的參照。
  • + 表示這是一個全新的資源名稱,需要建立並加入資源表。

有了 ID,就能在程式碼中取得該 View 的實例並加以操作:

 Button myButton = (Button) findViewById(R.id.my_button);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved