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

Android 佈局實戰:RelativeLayout 與 LinearLayout 的搭配應用

編輯:關於 Android 程式設計

Relative(相對佈局)顧名思義,是讓容器內的子元件依據彼此之間、或與容器之間的相對關係來定位的佈局方式。掌握它,能讓畫面的排列既靈活又精準。

Relative 的重要屬性

用於元件之間相對定位的屬性:

android:layout_above     置於給定 ID 的元件之上
android:layout_below     置於給定 ID 的元件之下
android:layout_toLeftOf  置於給定 ID 的元件之左
android:layout_toRightOf 置於給定 ID 的元件之右

相對於父容器的定位屬性:

android:layout_alignParentLeft    位於父容器左側
android:layout_alignParentRight   位於父容器右側
android:layout_alignParentTop     位於父容器頂部
android:layout_alignParentBottom  位於父容器底部

對齊相關屬性:

android:layout_alignBaseline  基線對齊給定 ID 的基線
android:layout_alignBottom    與給定 ID 底部對齊
android:layout_alignLeft      與給定 ID 左側對齊
android:layout_alignRight     與給定 ID 右側對齊
android:layout_alignTop       與給定 ID 頂部對齊
android:layout_centerHorizontal  置於水平中央
android:layout_centerInParent    置於父容器的水平與垂直中央
android:layout_centerVertical    置於垂直中央

特別提醒:不能在 RelativeLayout 容器本身與其子元件之間產生循環依賴。例如,當容器高度設為 WRAP_CONTENT 時,就不可再將子元件高度設為 ALIGN_PARENT_BOTTOM。

相對佈局實例一

以下是一個以純 RelativeLayout 完成的登入畫面:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10px"
    android:background="#00aa00">

    <TextView
        android:id="@+id/tv_heard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="12pt"
        android:text="XXX系統登錄界面" />

    <TextView android:id="@+id/tv_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_heard"
        android:layout_marginTop="30px"
        android:text="用戶名:" />

    <EditText android:id="@+id/txt_account"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_heard"
        android:layout_toRightOf="@id/tv_account"
        android:layout_alignBaseline="@id/tv_account" />

    <TextView android:id="@+id/tv_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_account"
        android:layout_marginTop="30px"
        android:text="密 碼:" />

    <EditText android:id="@+id/txt_pwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt_account"
        android:layout_toRightOf="@id/tv_pwd"
        android:layout_alignBaseline="@id/tv_pwd" />

    <Button android:id="@+id/btn_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt_pwd"
        android:layout_alignParentRight="true"
        android:layout_marginTop="30px"
        android:text="退出" />

    <Button android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt_pwd"
        android:layout_toLeftOf="@id/btn_exit"
        android:layout_marginTop="30px"
        android:layout_marginRight="50px"
        android:text="登錄" />
</RelativeLayout>

相對佈局實例二:與 LinearLayout 混合使用

將 RelativeLayout 與 LinearLayout 結合,往往能更快、更直觀地完成排版:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00aaff"
    android:padding="10px">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="12pt"
        android:text="XXX系統登錄界面" />

    <LinearLayout android:id="@+id/linear_one"
        android:layout_below="@id/tv_heard"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用戶名:" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:id="@+id/linear_second"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_below="@id/linear_one">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密 碼:" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:id="@+id/linear_third"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_below="@id/linear_second"
        android:gravity="right">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登錄" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30px"
            android:text="退出" />
    </LinearLayout>
</RelativeLayout>

透過這種「外層相對定位、內層線性排列」的組合策略,你能兼得兩種佈局的優勢,讓複雜畫面的實作事半功倍。

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