Android【佈局管理器】——相對佈局RelativeLayout

   相對佈局要比前面講的線性佈局和表格佈局要靈活一些,所以平常用得也是比較多的。相對佈局控件的位置是與其周圍控件的位置相關的,從名字可以看出來,這些位置都是相對的,確定出了其中一個控件的位置就可以確定另一個控件的位置了。

         本次實驗就是顯示如下的activity:

    

         其中只有2個button,1個textview,1個edittext。

        實現上面activity比較簡單,其xml代碼如下:

<span style="font-family:Comic Sans MS;font-size:18px;"><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:padding="10px" >

    <TextView
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_dis"
        tools:context=".MainActivity" />
    
       <EditText 
           android:id="@+id/edit"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content" 
        android:layout_below="@id/input"
        android:background="@android:drawable/editbox_background"
        />
       
       <Button 
           android:id="@+id/ok"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_below="@id/edit"
           android:layout_alignParentRight="true"
           android:layout_marginLeft="10px"
           android:text="@string/ok"           
           />
       
       <Button 
           android:id="@+id/cancel"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_below="@id/edit"
           android:layout_toLeftOf="@id/ok"
           android:text="@string/cancel"
           
           />

</RelativeLayout></span>

 

         在相對佈局中有如下屬性,解釋如下:

 

// 相對於給定ID控件

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

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

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

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



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

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

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

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

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

// 相對於父組件

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

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

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

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

// 居中

android:layout_centerHorizontal 如果爲true,將該控件的置於水平居中;

android:layout_centerVertical     如果爲true,將該控件的置於垂直居中;

android:layout_centerInParent   如果爲true,將該控件的置於父控件的中央;

// 指定移動像素

android:layout_marginTop      上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight   右偏移的值;

 

 

android:layout_alignParentRight="true" 
使當前控件的右端和父控件的右端對齊。這裏屬性值只能爲true或false,默認false。 
android:layout_marginLeft="10dip" 
使當前控件左邊空出相應的空間。 
android:layout_toLeftOf="@id/ok" 
使當前控件置於id爲ok的控件的左邊。 
android:layout_alignTop="@id/ok" 
使當前控件與id控件的上端對齊。 
padding表示填充,margin表示邊距 
可通過android:padding屬性進行設置,4個方向的邊距屬性爲android:paddingLeft, android:paddingRight, android:paddingTop, and android:paddingBottom. 

 

結論: 
*android:layout_marginBottom 
*android:layout_marginLeft 
*android:layout_marginRight 
*android:layout_marginTop 
上面幾個屬性的值是根據下面的相對位置的對象的值來做計算的,如果沒有相對的對象就以總體佈局來計算 
*android:layout_below 
*android:layout_above 
*android:layout_toLeftOf 
*android:layout_toRightOf 
*android:layout_alignTop 


*android:layout_centerHrizontal          //是否支持橫屏或豎屏 
*android:layout_centerVertical             //這個根據單詞的意思:中心垂直 
*android:layout_centerInparent         // 
android:layout_centerInParent="true"//居中在父對象 
android:layout_centerInParent="false" ... 瀏覽器不支持多窗口顯示,意思就是說所有頁面在單一窗口打開,這樣避免了頁面佈局控制顯示問題 
下面的相對於父的相對位置 
*android:layout_alignParentBottom 
*android:layout_alignParentLeft 
*android:layout_alignParentRight 
*android:layout_alignParentTop 
*android:layout_alignWithParentIfMissing 


  activity的相對佈局比較靈活,一些常見的屬性也比較多,用得多自然就會了。

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