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

Android 常用版面配置解析:LinearLayout 線性配置的屬性與權重設計

編輯:Android 開發教學

在 Android 的介面開發裡,版面配置(Layout)決定了子元件之間的排列與對齊關係。其中 LinearLayout 是最基礎、也最常被使用的一種線性配置容器。

一、LinearLayout 是什麼

LinearLayout 會把內部包含的子元件,依指定的方向排成一條直線。這些子元件可以是單純的 widget,也可以是另一個容器(Container)。排列方向由 android:orientation 決定,只有兩種取值:horizontal(水平)與 vertical(垂直)。

這種線性排列有個特性值得注意:所有子元件沿著同一個軸線依序堆疊,超出邊界的部分會被裁切、無法完整呈現。因此採用垂直排列時,每一列實際上只容納一個元件(不論它有多寬);而採用水平排列時,整列的高度就等於最高子元件的高度加上邊框高度。

LinearLayout 同時負責管理子元件之間的間距,以及它們的對齊方式——例如相對於某個特定元件做靠左、居中或靠右對齊。

二、常用 XML 屬性

android:baselineAligned:是否允許依內容基線進行對齊調整。

android:baselineAlignedChildIndex:當此線性配置作為基線對齊結構的一部分時,用來指定以哪一個子元件作為基線對齊的依據。

android:gravity:指定容器內部內容的對齊位置。

android:orientation:設定排列方向,可選 horizontal 或 vertical。

三、關鍵概念:layout_weight 權重

LinearLayout 另一項強大的能力,是允許子元件「填滿」螢幕上的剩餘空間,而分配依據就是權重屬性 android:layout_weight。

權重預設值為 0,代表元件依自身實際尺寸顯示,不參與剩餘空間的分配。一旦設定大於 0 的值,容器就會把剩餘可用空間依各元件的權重比例切分:某元件的權重佔全部權重的比例愈高,它能取得的剩餘空間就愈大。

舉例來說,若有三個文字方塊,其中兩個分別設定權重 2 與 1,那麼在配置完第三個(權重為 0)之後所剩下的空間,會有 2/3 分給權重 2 的元件、1/3 分給權重 1 的元件,第三個則維持原本大小。簡單記法:權重愈大,重要性愈高,佔用的剩餘空間也愈多。

四、實作範例

以下建立一個水平排列的 LinearLayout,放入兩個不同顏色背景的 TextView,並以權重 2:1 分配空間。

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

    <!--
        常用屬性速查
        android:id            為元件指定識別碼
        android:text          指定元件顯示的文字
        android:gravity       指定內容在元件內的位置,例如居中、靠右
        android:textSize      指定文字大小
        android:background    指定背景色,採 RGB 命名法
        android:width         指定寬度
        android:height        指定高度
        android:padding       指定四邊內距
        android:paddingLeft   指定左內距
        android:paddingRight  指定右內距
        android:paddingTop    指定上內距
        android:paddingBottom 指定下內距
        android:singleLine    設為 true 時,內容強制單行顯示
        android:layout_weight 設定在 LinearLayout 中的填充權重
    -->

    <TextView
        android:id="@+id/first"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:text="第一個 TextView"
        android:textSize="20pt"
        android:gravity="center_vertical"
        android:background="#aa0000"
        android:paddingLeft="10dip"
        android:paddingRight="30dip"
        android:paddingTop="10dip"
        android:paddingBottom="30dip"
        android:layout_weight="2" />

    <TextView
        android:id="@+id/second"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:text="第二個 TextView"
        android:textSize="20pt"
        android:gravity="center_vertical"
        android:background="#0000aa"
        android:paddingLeft="10dip"
        android:paddingRight="30dip"
        android:paddingTop="10dip"
        android:paddingBottom="30dip"
        android:layout_weight="1" />

</LinearLayout>

執行後可以看到兩個方塊並排,紅色方塊佔據約三分之二的剩餘寬度,藍色方塊佔三分之一,這正是權重比例分配最直觀的效果。

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