上一篇介紹過如何使用LinearLayout進行元件佈局,但是單靠一種排版方式,設計出來的介面並不豐富,所以Android也有提供其它的排版方式,本篇將介紹如何使用RelativeLayout(相對佈局)進行排版。其它的排版方式會陸陸續續的介紹!!

  • 使用RelativeLayout進行元件佈局

   跟上一篇不同的地方是將包覆元件的容器改成RelativeLayout,本範例使用一個RelativeLayout來包覆4個Button元件,並定義一些元件屬性完成元件佈局.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:id="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button1"
    />
<Button
    android:id="@+id/Button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2"
    android:layout_below="@+id/Button1"
    android:layout_toRightOf="@+id/Button1"
    />
<Button
    android:id="@+id/Button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button3"
    android:layout_toRightOf="@+id/Button2"
    android:layout_above="@+id/Button2"
    />
 <Button
    android:id="@+id/Button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button4"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    />
</RelativeLayout>

 

屬性 說明
android:id 該屬性指定一個辨識符號給元件,並自動在R.java中建立索引,透過此id可用來調用元件.
android:layout_width 該屬性定義元件的寬度,可使用的屬性值有"fill_parent"(填滿容器空間)、"wrap_content"(根據內部內容延展至適當大小)、"match_parent"(大致上功能與"fill_parent"相同,2.2版本才可使用).
android:layout_height 該屬性定義元件的高度,可使用屬性值同上.
android:text 該屬性可設定文字顯示在元件上.
android:layout_above 將此元件置於"指定元件"(使用元件id指定)上方.
android:layout_below 將此元件置於"指定元件"(使用元件id指定)下方.
android:layout_toLeftOf 將此元件置於"指定元件"(使用元件id指定)左方.
android:layout_toRightOf 將此元件置於"指定元件"(使用元件id指定)右方.
android:layout_alignParentTop 將此元件對齊於佈局畫面上邊線,屬性值為true、false.
android:layout_alignParentRight 將此元件對齊於佈局畫面右邊線,屬性值為true、false.
android:layout_alignParentLeft 將此元件對齊於佈局畫面左邊線,屬性值為true、false.
android:layout_alignParentBottom 將此元件對齊於佈局畫面底線,屬性值為true、false.

 

  • 使用RelativeLayout元件佈局結果
執行結果

   ※自行調整各元件的屬性值,看看會產生什麼不同的排版方式.

arrow
arrow
    全站熱搜

    Felix 發表在 痞客邦 留言(0) 人氣()