Android 佈局詳解 -二相對佈局(Relative Layout)以及重要屬性

二Relative Layout

Relative Layout佈局:相對位置佈局,的幾個重要屬性 android

  第一類 : 屬性值爲 true 或 false 佈局

  •   android:layout_centerHrizontal           水平居中
  •   android:layout_centerVertical             垂直居中
  •   android:layout_centerInparent            相對於父元素徹底居中
  •   android:layout_alignParentBottom       貼緊父元素的下邊緣
  •   android:layout_alignParentLeft            貼緊父元素的左邊緣
  •   android:layout_alignParentRight          貼緊父元素的右邊緣
  •   android:layout_alignParentTop            貼緊父元素的上邊緣
  •   android:layout_alignWithParentIfMissing  若找不到兄弟元素以父元素作參照物

 

第二類:屬性值必須爲 id 的引用名「 @id/id-name 」 spa

 

  •   android:layout_below           在某元素的下方
  •   android:layout_above           在某元素的上方
  •   android:layout_toLeftOf       在某元素的左邊
  •   android:layout_toRightOf     在某元素的右邊
  •   android:layout_alignTop       本元素的上邊緣和某元素的的上邊緣對齊
  •   android:layout_alignLeft      本元素的左邊緣和某元素的的左邊緣對齊
  •   android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
  •   android:layout_alignRight    本元素的右邊緣和某元素的的右邊緣對齊

 

  第三類:屬性值爲具體的像素值,如 30dip , 40px code

 

  •   android:layout_marginBottom  離某元素底邊緣的距離
  •   android:layout_marginLeft       離某元素左邊緣的距離
  •   android:layout_marginRight     離某元素右邊緣的距離
  •   android:layout_marginTop       離某元素上邊緣的距離

Relative Layout佈局:相對位置佈局,相似於Word中的位置對齊,總共有九個方位,以下圖所示 xml

能夠分別對應Word中的九個方位,以及還能夠設置相似CSS中的組件與組件之間的外邊距,如margin便是設置這一項,注意layout_margin這一項設置像素之後,則是設置的上下左右四個方位的值,若是隻想設置某一邊的話,則使用下面的marginLeft……之一, 
設置組件內文字與組件邊距可用屬性padding,以下圖: ip

下面,咱們來看看佈局文件的代碼與詳解 utf-8

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label"
        android:background="@android:drawable/editbox_background" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/entry"
        android:layout_marginLeft="10dip"
        android:text="OK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/ok"
        android:layout_toLeftOf="@id/ok"
        android:text="Cancel" />

</RelativeLayout>

最後的效果圖爲: it

還需知道:

1.android:paddingLeft與android:layout_marginLeft的區別: io

      padding margin都是邊距的含義,關鍵問題得明白是什麼相對什麼的邊距.
      padding是控件的內容相對控件的邊緣的邊距.
      layout_margin是控件邊緣相對父空間的邊距. class

2.android: gravityandroid:layout_gravity區別

  android:gravity 屬性是對該view 內容的限定.好比一個button 上面的text.  你能夠設置該text 在view的靠左,靠右等位置.該屬性就幹了這個.
      android:layout_gravity是用來設置該view相對與起父view 的位置.好比一個button 在linearlayout裏,你想把該button放在靠左靠右等位置就能夠經過該屬性設置.

這樣就解釋了,有什麼咱們弄個最外佈局,而後裏面包了幾個佈局,若是要使這幾個佈局都靠底,就能夠在最外佈局的屬性裏設置androi:gravity="botton"  由於gravity是對裏面的內容起做用.

相關文章
相關標籤/搜索