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

Android 教學:用 LinearLayout 打造線性版面配置

編輯:關於 Android 開發

LinearLayout 是 ViewGroup 的一種,其中的子 View 會依照線性方式排列,方向可以是垂直(vertical)或水平(horizontal)。本文透過一個 HelloLinearLayout 範例,示範如何透過巢狀的 LinearLayout 組合出多層次的畫面。

一、建立專案

新增一個名為 HelloLinearLayout 的專案,並開啟 res/layout/main.xml 進行修改。

二、撰寫版面配置

最外層是一個 orientation 設為 vertical 的 LinearLayout,內含兩個子 LinearLayout:第一個採水平排列,第二個採垂直排列,每個子 LinearLayout 又各自包含數個 TextView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="red" android:gravity="center_horizontal"
            android:background="#aa0000" android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:layout_weight="1"/>
        <TextView android:text="green" android:gravity="center_horizontal"
            android:background="#00aa00" android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:layout_weight="1"/>
        <TextView android:text="blue" android:gravity="center_horizontal"
            android:background="#0000aa" android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:layout_weight="1"/>
        <TextView android:text="yellow" android:gravity="center_horizontal"
            android:background="#aaaa00" android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="row one" android:textSize="15pt"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView android:text="row two" android:textSize="15pt"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView android:text="row three" android:textSize="15pt"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView android:text="row four" android:textSize="15pt"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

三、載入版面配置

開啟 HelloLinearLayout.java,確認它載入了 main.xml:

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

setContentView(int) 方法負責載入 Activity 的版面配置,資源 ID — R.layout.main 指向 res/layout/main.xml。

四、觀察結果

執行應用後即可看到畫面。不妨動手修改 android:layout_weight(搭配 layout_width/layout_height)的值,觀察各個 View 的顯示比例如何隨之改變,這也是理解權重配置最快速的方式。

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