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

Android XML 版面配置屬性速查:LinearLayout 與 RelativeLayout 常用寫法

編輯:關於 Android 程式設計

寫 Android 版面時,真正花時間的往往不是結構,而是背誦那一長串屬性名稱。以下把 LinearLayout 與 RelativeLayout 的常用屬性整理成一份可直接查閱的對照表。

通用屬性

標識控件

android:id="@+id/btn1"      // Java 中以 findViewById(R.id.btn1) 取得

寬與高

android:layout_width="80dp"             // 固定值
android:layout_width="wrap_content"     // 依內容決定
android:layout_width="match_parent"     // 填滿父容器

android:layout_height="80dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"

排列方向(LinearLayout 專用)

android:orientation="horizontal"    // 水平排列
android:orientation="vertical"      // 垂直排列

外边距 margin:與父容器或兄弟節點的距離

android:layout_marginLeft="5dp"     // 左
android:layout_marginRight="5dp"    // 右
android:layout_marginTop="5dp"      // 上
android:layout_marginBottom="5dp"   // 下

內距 padding:內容與自身邊界的距離

android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"

gravity 與 layout_gravity:最容易搞混的一對

兩者名字只差幾個字,意義完全不同:

  • android:gravity:設定 View 內部內容的對齊方式。例如 TextView 的文字靠左或置中。
  • android:layout_gravity:設定 View 本身相對於父容器的位置(僅 LinearLayout 有效)。
android:gravity="center"                // 內容水平垂直置中
android:gravity="center_horizontal"     // 內容水平置中
android:gravity="left|bottom"           // 內容靠左下

android:layout_gravity="center_vertical"
android:layout_gravity="left"
android:layout_gravity="left|bottom"

TextView 文字樣式

android:text="@string/text1"        // 建議抽到 strings.xml,不要寫死字串
android:textSize="20sp"             // 文字一律用 sp,才能跟隨系統字級縮放
android:textColor="#ff123456"       // ARGB 格式
android:textStyle="bold"            // normal / italic / bold / bold_italic

可見性與背景

android:visibility="visible"    // 顯示
android:visibility="invisible"  // 隱藏,但仍占據版面位置
android:visibility="gone"       // 隱藏,且不占版面空間

android:background="@drawable/img_bg"

invisible 與 gone 的差異在實務上很關鍵:切換到 gone 會觸發重新布局(measure/layout),因此頻繁切換時要注意效能;而 invisible 只是不繪製。

SeekBar 相關

android:progressDrawable="@drawable/seekbar_img"   // 進度條整體背景
android:thumb="@drawable/thumb"                    // 可拖動的圓鈕
android:max="60"                                   // 最大值

RelativeLayout 專屬:對齊父容器

android:layout_alignParentLeft="true"      // 靠父容器左側
android:layout_alignParentRight="true"     // 靠父容器右側
android:layout_alignParentTop="true"       // 靠父容器頂部
android:layout_alignParentBottom="true"    // 靠父容器底部

android:layout_centerHorizontal="true"     // 水平居中
android:layout_centerVertical="true"       // 垂直居中
android:layout_centerInParent="true"       // 完全居中

RelativeLayout 專屬:相對於其他控件

android:layout_toRightOf="@id/button1"   // 在 button1 右側(不貼齊)
android:layout_toLeftOf="@id/button1"    // 在 button1 左側
android:layout_below="@id/button1"       // 在 button1 下方
android:layout_above="@id/button1"       // 在 button1 上方

對齊邊緣

android:layout_alignTop="@id/button1"       // 頂邊對齊
android:layout_alignBottom="@id/button1"    // 底邊對齊
android:layout_alignLeft="@id/button1"      // 左邊對齊
android:layout_alignRight="@id/button1"     // 右邊對齊

LinearLayout 專屬:權重

android:layout_weight="1"

weight 決定剩餘空間的分配比例,常見用法是搭配 layout_width="0dp"(水平排列時)讓多個元件等寬。若改用 wrap_content 加 weight,元件會先依內容佔位再分配剩餘空間,寬度就不會相等,這是初學者常遇到的問題。

幾點現代化的建議

單位方面,版面尺寸請一律用 dp(dip 是舊寫法,效果相同),文字用 sp,避免使用 px 或 pt,否則在不同密度的螢幕上視覺大小會不一致。

結構方面,RelativeLayout 的巢狀層數容易失控,效能不如預期。新專案建議改用 ConstraintLayout:它能用約束取代巢狀,多數原本需要 RelativeLayout 的場景都能扁平化處理,並支援百分比寬度、比例鏈(chain)、barrier 等進階排版能力。

最後記得把寫死的字串與顏色抽到 strings.xml 與 colors.xml,這不只是為了多語系,也是主題切換與夜間模式的必要前提。

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