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

Android 資源指南(26):Layout 版面資源的語法、元素與屬性

編輯:Android 開發實例

版面配置(layout)資源定義 Activity 的使用者介面,或介面中某個控制項的排列結構。本文對應 Android 3.2 r1。

基本資訊

  • 檔案位置:res/layout/filename.xml,檔名即為資源 ID。
  • 編譯後型別:指向 View 或其子類別的資源指標。
  • 資源引用:Java 用 R.layout.filename,XML 用 @layout/filename。

語法結構

<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+[package:]id/resource_name"
    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >

    <View
        android:id="@+[package:]id/resource_name"
        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>

    <ViewGroup>
        <View/>
    </ViewGroup>

    <include layout="@layout/layout_resource"/>
</ViewGroup>

注意:根元素可以是 ViewGroup、View 或 <merge>,但只能有一個根元素,且必須帶上 xmlns:android 命名空間屬性。

元素說明

<ViewGroup>

作為 View 的容器,不同的 ViewGroup 種類以不同方式排列子元素,常見的有 LinearLayout、RelativeLayout 與 FrameLayout。

並非所有 ViewGroup 的派生類別都能容納任意嵌套的 View:有些 ViewGroup 是 AdapterView 的實作,只能容納 Adapter 類型的子元素。

主要屬性:

  • android:id(資源 ID):為元素指定唯一資源名稱,供程式引用。
  • android:layout_height(度量或關鍵字,必填):高度。
  • android:layout_width(度量或關鍵字,必填):寬度。

寬高可用度量值、度量資源(dimension resource),或 "fill_parent"、"wrap_content" 兩個關鍵字。基類與各子類別另有許多專屬屬性,完整清單請查閱對應類別的參考文件,例如 LinearLayout XML attributes。

<View>

一個獨立的 UI 控制項,通常即所謂的 widget,常見種類有 TextView、Button 與 CheckBox。屬性與 ViewGroup 的 android:id、android:layout_height、android:layout_width 相同,另外各子類別還有自己的專屬屬性,例如 TextView XML 屬性。

<requestFocus>

把螢幕的初始焦點設在父元素上。任何代表 View 的元素都能包含這個空元素,但同一個檔案中只能出現一次。

<include>

把另一個版面檔案包含進來。屬性如下:

  • layout(版面資源,必填):要引用的版面資源。
  • android:id(資源 ID):覆寫被包含版面中根 view 的 ID。
  • android:layout_height/android:layout_width(度量或關鍵字):覆寫被包含版面中根 view 的尺寸,但僅在兩者同時指定時才生效。

凡是根元素支援的屬性,都能寫在 <include> 中並覆寫原值。

重要提醒:要覆寫版面的長寬,必須同時覆寫 android:layout_height 與 android:layout_width,只改其中一邊不會生效。其他版面屬性(例如 weight)仍舊繼承自原本的版面。

另一種動態包含版面的方式是 ViewStub。它是輕量級的 View,在真正被填充之前不佔用版面空間;填充時才把 android:layout 指定的版面動態載入。

<merge>

這個元素不會顯示在版面的層次結構圖中。當明確知道本版面會被放進某個父 View 時,可以用 <merge> 當根元素來包住子元素;若希望本版面被其他版面用 <include> 引入時不再多產生一層 ViewGroup 容器,這個做法特別有用。

android:id 的寫法

通常寫成 "@+id/name",其中的加號表示這是一個新的資源 ID;若 R.java 中還不存在,aapt 工具會自動新增一個整數常數:

<TextView android:id="@+id/nameTextbox"/>

此後 nameTextbox 就成為這個元素的資源 ID,可在程式中引用:

findViewById(R.id.nameTextbox);

回傳值即為該 TextView 物件。若 ID 資源已經定義過(且尚未被使用),則可以去掉加號,直接把既有 ID 指派給 View 元素。

寬高的合法值

  • match_parent:與父元素等寬/等高(填滿)。自 API Level 8 起加入,用以取代 fill_parent。
  • fill_parent:與父元素等寬/等高(填滿)。
  • wrap_content:剛好容納本元素內容所需的大小。

當然也可以直接使用 Android 支援的度量單位:px、dp、sp、pt、in、mm。

自訂 View 元素

自訂的 View 與 ViewGroup 可以像標準版面元素一樣放進 layout,也能在 XML 元素中定義其支援的屬性。

完整範例

存放於 res/layout/main_activity.xml:

<?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="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>

在 Activity 的 onCreate() 中載入版面:

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

延伸閱讀:Declaring Layout、View、ViewGroup。

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