Android--(10)--詳解相對佈局(RelativeLayout)

RelativeLayout特點:組件的位置總是相對於兄弟組件或者父容器來決定。如果A的位置由B決定,必須先定義組件B,再定義組件A;

相對佈局中的四種控件位置屬性:
(1)與兄弟空間外側的四個方位對齊
    android:layout_toLeftOf=”@id/text1” 該控件在text1的左邊;
    android:layout_toRightOf=”@id/text1” 該控件在text1的右邊;
    android:layout_above=”@id/text1” 該控件在text1的上邊;
    android:layout_below=”@id/text1” 該控件在text1的下邊;
(2)與兄弟控件的四個邊緣對齊
    android:layout_alignBottom=”@id/text1” 該控件下邊線與指定id控件的下邊線對齊
    android:layout_alignTop=”@id/text1” 該控件上邊線與指定id控件的上邊線對齊
    android:layout_alignLeft=”@id/text1” 該控件左邊線與指定id控件的左邊線對齊
    android:layout_alignRight=”@id/text1” 該控件右邊線與指定id控件的右邊線對齊
    android:layout_alignBaseline=”@id/text1” 兩控件的文字基線對齊
(3)與父控件的四個邊緣對齊
     android:layout_alignParentBottom=”true” 是否相對於父控件的下方;
     android:layout_alignParentTop=”true” 是否相對於父控件的上方;
     android:layout_alignParentLeft=”true” 是否相對於父控件的左方;
     android:layout_alignParentRight=”true” 是否相對於父控件的右方;

(4)與父控件的中央對齊
     android:layout_centerInParent=”true” 子類控件與父類控件即水平又垂直居中;
     android:layout_centerHorizental=”true” 子類控件與父類控件水平居中;
     android:layout_centerVertical=”true” 子類控件與父類控件垂直居中;

使用相對佈局實現登錄界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView 
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用戶名:"
        />

    <EditText
        android:id="@+id/e1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/t1"
        tools:ignore="TextFields" />

    <TextView 
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/e1"
        android:text="密碼:"
        />
    <EditText 
        android:id="@+id/e2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/t2"
        />
    <Button 
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/e2"    //設按鈕位於第二個文本框下方;
        android:layout_alignRight="@+id/e2"    //設置按鈕與第二個文本框的左邊框相對齊;
        android:text="登錄"       
        />
</RelativeLayout>

//學習記錄,僅代替筆記!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章