1. 程式人生 > >Android學習筆記 九 Activity RelativeLayout

Android學習筆記 九 Activity RelativeLayout

指定widget在container的相對位置,包括:android:layout_alignParentTop, android:layout_alignParentBottom, android:layout_alignParentLeft, android:layout_alignParentRight, android:layout_centerHorizontal, android:layout_centerVertical, android:layout_centerInParent,他們的值是false|true。

如果是相對其他widget的位置,可以使用:android:layout_above,

android:layout_below, android:layout_toLeftOf,android:layout_toRightOf。與其他widget位置對齊,可以使用:android:layout_alignTop,android:layout_alignBottom, android:layout_alignLeft,android:layout_alignRigh, android:layout_alignBaseline,最後一個通常用於label的對齊。語法格式例子:layout_toRightOf="@id/widget_a"

我們將建立下面的Activity,Android XML如下:

<?xml version="1.0" encoding="utf-8"?><!-- 我們採用RelativeLayout的佈局,並設定了pad的留邊,需要注意pad屬於widget的有效範圍 --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" >

<!-- textview是我們第一個基準widget,我們設定了android:paddingTop="15px"。-->    <TextView android:id="@+id/label"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="URL:"      android:paddingTop="15px" /><!-- 在label的右面有一edittext,填滿餘下的空間,並和label進行對齊-->    <EditText android:id="@+id/entry"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:layout_toRightOf="@id/label"      android:layout_alignBaseline="@id/label"       /><!-- 在edittext的下面並對齊最右方有一個OK button-->    <Button android:id="@+id/ok"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_below="@id/entry"      android:layout_alignRight="@id/entry"      android:text="OK"       /><!-- 在OK按鍵的左邊增加一個Cancel button,並對齊。如果我們要求和OK的button之間增加間距,我們可以在下面增加設定android:laytou_marginRight="10px"-->    <Button android:id="@+id/cancel"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_toLeftOf="@id/ok"      android:layout_alignTop="@id/ok"      android:text="Cancel"       /></RelativeLayout>

textview是我們第一個基準widget,我們設定了android:paddingTop="15px",否則由於後面我們按此進行對齊,editiew會向上移並被chip最上端。由於editview上移,也導致了和下面button之間的間距過大。android是根據網格來安排widget的位置,每個widget都有一個確定的高度並匹配網格,如果widget被拉高,因為網格定位的緣故,button的相對位置並不會被擡高。同樣的如果我們設定 android:paddingTop="30px",editview的位置下沉,同樣由於網格的緣故,下面的button不會隨著下沉,將和eidtiew的位置有所重疊,如圖所示。

 

然則,我們怎麼知道要給label設定15px,如果佈局都需要根據這樣的經驗值,就相當鬱悶,另一個解決方式,就是在定義edittext之前,就將label的對應位置根據其進行調整,然後再定義edittext。這在1.5版本之前有問題,因此我們需要設定AndroidManifest.xml,設定我們的最小執行版本環境,例如2.2,我們在manifest中設定:<uses-sdk android:minSdkVersion="8" /> 並且在default.properties檔案中設定target=android-8,在Android的XML檔案,處理如下:

    <TextView android:id="@+id/label"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="URL:"       android:layout_alignBaseline="@+id/entry"       android:layout_alignParentLeft="true"       />    <EditText android:id="@id/entry"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:layout_toRightOf="@id/label"       android:layout_alignParentTop="true"        />