Android佈局之RelativeLayout學習

先來看一下相對佈局的主要屬性有哪些:

Android:layout_above 將該控件的底部置於給定ID的控件之上;

Android:layout_below 將該控件的底部置於給定ID的控件之下;

Android:layout_toLeftOf    將該控件的右邊緣與給定ID的控件左邊緣對齊;

Android:layout_toRightOf  將該控件的左邊緣與給定ID的控件右邊緣對齊;

-------------------------------------------------------------------------

Android:layout_alignParentTop      如果爲true,將該控件的頂部與其父控件的頂部對齊;

Android:layout_alignParentBottom 如果爲true,將該控件的底部與其父控件的底部對齊;

Android:layout_alignParentLeft      如果爲true,將該控件的左部與其父控件的左部對齊;

Android:layout_alignParentRight    如果爲true,將該控件的右部與其父控件的右部對齊;

----------------------------------------------------------------------------------

Android:layout_alignBaseline  將該控件的baseline與給定ID的baseline對齊;

Android:layout_alignTop        將該控件的頂部邊緣與給定ID的頂部邊緣對齊;

Android:layout_alignBottom   將該控件的底部邊緣與給定ID的底部邊緣對齊;

Android:layout_alignLeft        將該控件的左邊緣與給定ID的左邊緣對齊;

Android:layout_alignRight      將該控件的右邊緣與給定ID的右邊緣對齊;

-------------------------------------------------------------------------------

代碼示例:

先看效果:

XML代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <TextView 
        android:id="@+id/username"
        android:textSize="16dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/username"
        />
    <EditText 
        android:id="@+id/input_user"
        android:hint="@string/input_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        />
    <TextView 
        android:id="@+id/password"
        android:textSize="16dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/input_user"
        android:text="@string/password"
        />

    <EditText
        android:id="@+id/input_pwd"
        android:hint="@string/input_pwd"
        android:inputType="textPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
         />

    <Button 
        android:text="@string/ok"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:id="@+id/ok"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/input_pwd"
        />

    <Button
        android:id="@+id/cancl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input_pwd"
        android:layout_toLeftOf="@+id/ok"
        android:text="@string/cancl"
        android:textSize="16dp" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button 
        android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="@string/button"
        />
    <Button 
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toRightOf="@id/button"
        android:layout_below="@id/button"
        android:text="@string/button1"
        />
    <Button 
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_below="@id/button"
        android:layout_toLeftOf="@id/button"
        android:text="@string/button2"
        />
    <Button 
        android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toRightOf="@id/button"
        android:layout_above="@id/button"
        android:text="@string/button3"
        />
    <Button 
        android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toLeftOf="@id/button"
        android:layout_above="@id/button"
        android:text="@string/button4"
        />
    
</RelativeLayout>

工具:eclipse3.7+adt 20測試成功

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