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

Android 基礎教程(二):五大布局對象解析

編輯:開發入門

Android 提供了多種布局方式,用來安排畫面中的元件。最常見的五種布局分別是 FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout 與 TableLayout。

Android 布局效果圖

FrameLayout

FrameLayout 是最簡單的布局之一,通常只放入一個主要元件。所有子元件會疊放在同一位置,後加入的元件會覆蓋前面的元件。

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:text="button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="textview"
        android:textColor="#0000ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

LinearLayout

LinearLayout 會依照水平或垂直方向排列子元件。它可以透過 weight 屬性分配剩餘空間,讓畫面配置更有彈性。

AbsoluteLayout

AbsoluteLayout 允許元件使用精確的 x、y 座標定位。這種方式較為剛性,在不同螢幕尺寸上容易造成相容性問題,因此不建議大量使用。

RelativeLayout

RelativeLayout 可以讓元件相對於父元件或其他元件定位,例如靠左、靠右、置中或位於某個元件下方,非常適合用來建立靈活的畫面結構。

TableLayout

TableLayout 會將子元件安排成表格形式,通常由多個 TableRow 組成。每一列可以包含多個儲存格,但不像 HTML 表格那樣支援跨欄合併。

實際開發時,建議優先使用 LinearLayout、RelativeLayout 或 ConstraintLayout 等更具弹性的布局方式,以提升不同裝置上的適配能力。

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