close
  • RelativeLayout

RelativeLayout–相對布局,顧名思義就是可以透過相對位置來設定布局內各個元件的位置,其元件設定的位置可以是相對於整個RelativeLayout布局或者是相對於其他元件的位置。

 

下表為一些屬性設定介紹(出處:Corn-Android學習手札iteye_android

屬性 說明
 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.
 android:layout_centerHorizontal

  將該元件水平居中於整個布局畫面,屬性值為true、false

 android:layout_centerVertical   將該元件垂直居中於整個布局畫面,屬性值為true、false
 android:layout_centerInParent   將該元件水平及垂直均居中於整個布局畫面,屬性值為true、false
 android:layout_marginBottom   將該元件離布局畫面底邊多少距離,屬性質為具體的像數值,如10dp、10px
 android:layout_marginLeft   將該元件離布局畫面左邊多少距離,屬性質為具體的像數值,如10dp、10px
 android:layout_marginRight   將該元件離布局畫面右邊多少距離,屬性質為具體的像數值,如10dp、10px
 android:layout_marginTop   將該元件離布局畫面上邊多少距離,屬性質為具體的像數值,如10dp、10px
 android:layout_alignParentBottom   將該元件對齊布局畫面的底邊,屬性值為true、false
 android:layout_alignParentLeft   將該元件對齊布局畫面的左邊,屬性值為true、false
 android:layout_alignParentRight   將該元件對齊布局畫面的右邊,屬性值為true、false
 android:layout_alignParentTop   將該元件對齊布局畫面的上邊,,屬性值為true、false

註:要特別注意,由於Android的預設各元件會自動靠左及上邊,所以在使用『android:layout_marginBottom』或『android:layout_marginRight』則須搭配『android:layout_alignParentBottom』或『android:layout_alignParentRight』才會有效果。

 

RelativeLayout:

<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:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_below="@+id/button1"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_alignParentRight="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 4"
        android:layout_marginBottom="50dp"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

 RelativeLayout  

 

觀看其它種Layout:

 

參考資料:Corn-Android學習手札iteye_android屌絲技術之旅

arrow
arrow
    全站熱搜

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