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

Android GUI 容器布局入門:LinearLayout

編輯:關於 Android 開發

Android 中有三個經常使用的布局容器:LinearLayout、RelativeLayout 與 TableLayout。本文先從最基礎的 LinearLayout(線性布局)談起。

LinearLayout 會讓其中的組件或子容器沿水平或垂直方向線性排列。它有幾個基本屬性值得掌握:

  • android:orientation:值為 horizontal 時水平排列,值為 vertical 時垂直排列。
  • android:layout_width 與 android:layout_height:控制每個組件的寬與高。
  • android:layout_weight:控制組件之間的比例分配。
  • android:layout_gravity:控制組件的對齊與排列方式。
  • android:padding:設定內部的邊緣間距。

以下是一個簡單的 LinearLayout XML 文件,示範了如何在線性容器中嵌套 RadioGroup 與 RadioButton:

<?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"
    >
    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px">
        <RadioButton
            android:text="RadioButton1" />
        <RadioButton
            android:text="RadioButton2" />
    </RadioGroup>
    <RadioGroup
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px">
        <RadioButton
            android:text="RadioButton1" />
        <RadioButton
            android:text="RadioButton2" />
        <RadioButton
            android:text="RadioButton3" />
    </RadioGroup>
</LinearLayout>

透過 LinearLayout,我們可以輕鬆地將介面元素排列成整齊的水平或垂直隊列,是建構 Android 界面的基礎配置。掌握它之後,再搭配 RelativeLayout 與 TableLayout,就能應對更複雜的布局需求。

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