Android開發之詳解五大布局

來源:互聯網
上載者:User

標籤:android   http   io   ar   for   sp   div   c   on   

 

為了適應各式各樣的介面風格,Android系統提供了5種布局,這5種布局分別是:

LinearLayout(線性布局)

TableLayout(表格版面配置)

RelativeLayout(相對布局)

AbsoluteLayout(絕對布局)

FrameLayout(架構布局)

利用這五種布局,可以在螢幕上將控制項隨心所欲的擺放,而且控制項的大小和位置會隨著螢幕大小的變化作出相應的調整。下面是這五個布局在View的繼承體系中的關係:
<ignore_js_op> 


一,LinearLayout(線性布局)

    在一個方向上(垂直或水平)對齊所有子項目
一個垂直列表每行將只有一個子項目(無論它們有多寬)
一個水平列表只是一列的高度(最高子項目的高度來填充)

下面是一個簡單的線性布局的例子:
<ignore_js_op>

  1. 01 <?xml version="1.0" encoding="utf-8"?> 
  2. 02 <LinearLayout 
  3. 03 xmlns:android="http://schemas.android.com/apk/res/android" 
  4. 04 android:layout_width="match_parent" 
  5. 05 android:layout_height="match_parent" android:orientation="vertical"> 
  6. 06 <EditText android:text="EditText" 
  7. 07   
  8. 08 android:id="@+id/editText1" 
  9. 09   
  10. 10 android:layout_height="wrap_content" 
  11. 11   
  12. 12 android:layout_width="fill_parent"> 
  13. 13   
  14. 14 </EditText> 
  15. 15 <LinearLayout android:id="@+id/linearLayout1" 
  16. 16   
  17. 17 android:layout_height="fill_parent" 
  18. 18   
  19. 19 android:layout_width="fill_parent" 
  20. 20   
  21. 21 android:gravity="right"> 
  22. 22 <Button android:id="@+id/button2" 
  23. 23   
  24. 24 android:text="Button" 
  25. 25   
  26. 26 android:layout_width="wrap_content" 
  27. 27   
  28. 28 android:layout_height="wrap_content"></Button> 
  29. 29 <Button android:text="Button" 
  30. 30   
  31. 31 android:id="@+id/button1" 
  32. 32   
  33. 33 android:layout_width="wrap_content" 
  34. 34   
  35. 35 android:layout_height="wrap_content"></Button> 
  36. 36 </LinearLayout> 
  37. 37 </LinearLayout>
複製代碼

最外層布局為垂直線性布局,寬度為整個螢幕(fill_parent),高度為剛好適合子控制項(wrap_content)。然後依次添加一個EditText,一個水平布局的LinearLayout,在這個線性布局裡面,擺放兩個Button,該線性布局的gravity屬性設定為”right”,所以裡面的兩個Button靠右顯示。

二,TableLayout(表格版面配置)

把子項目放入到行與列中
不顯示行、列或是儲存格邊界線
儲存格不能橫跨行,如HTML中一樣
表格版面配置模型以行列的形式管理子控制項,每一行為一個TableRow的對象,當然也可以是一個View的對象。TableRow可以添加子控制項,每添加一個為一列。

android:layout_colum官方解釋:The index of the column in which this child should be,也即是設定該控制項在TableRow中所處的列。
android:layout_span官方解釋:Defines how many columns this child should span,也即是設定該控制項所跨越的列數。

android:collapseColumns官方解釋:The 0 based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5.也即是將TableLayout裡面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。

android:stretchColumns官方解釋:The 0 based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. You can stretch all columns by using the value “*” instead. Note that a column can be marked stretchable and shrinkable at the same time.也即是設定指定的列為可伸展的列,可伸展的列會盡量伸展以填滿所有可用的空間,若有多列需要設定為可伸展,請用逗號將需要伸展的列序號隔開。

android:shrinkColumns官方解釋:The 0 based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5. You can shrink all columns by using the value “*” instead. 設定指定的列為可收縮的列。當可收縮的列太寬以至於讓其他列顯示不全時,會縱向延伸空間。當需要設定多列為可收縮時,將列序號用逗號隔開。

下面用一個例子簡單說明TableLayout的用法:
<ignore_js_op>

  1. 01 <?xml version="1.0" encoding="utf-8"?> 
  2. 02 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. 03 android:layout_width="fill_parent" 
  4. 04 android:layout_height="fill_parent" 
  5. 05 android:stretchColumns="1"> 
  6. 06 <TableRow> 
  7. 07 <TextView 
  8. 08 android:layout_column="1" 
  9. 09 android:padding="3dip" android:text="Row1"/> 
  10. 10 <TextView 
  11. 11 android:text="1" 
  12. 12 android:gravity="right" 
  13. 13 android:padding="3dip" /> 
  14. 14 </TableRow> 
  15. 15 <View 
  16. 16 android:layout_height="2dip" 
  17. 17 android:background="#FF909090" /> 
  18. 18 <TableRow> 
  19. 19 <TextView 
  20. 20 android:text="*" 
  21. 21 android:padding="3dip" /> 
  22. 22 <TextView 
  23. 23 android:text="Row12" 
  24. 24 android:padding="3dip" /> 
  25. 25 <TextView 
  26. 26 android:text="2" 
  27. 27 android:gravity="right" 
  28. 28 android:padding="3dip" /> 
  29. 29 </TableRow> 
  30. 30 <View 
  31. 31 android:layout_height="2dip" 
  32. 32 android:background="#FF909090" /> 
  33. 33 <TableRow> 
  34. 34 <TextView 
  35. 35 android:layout_column="1" 
  36. 36 android:text="Row13" 
  37. 37 android:padding="3dip" /> 
  38. 38 </TableRow> 
  39. 39 </TableLayout>
複製代碼

三、RelativeLayout(相對布局)

相對布局的子控制項會根據它們所設定的參照控制項和參數進行相對布局。參照控制項可以是父控制項,也可以是其它子控制項,但是被參照的控制項必須要在參照它的控制項之前定義。下面是一個簡單的例子:
<ignore_js_op>

  1. 01 <?xml version="1.0" encoding="utf-8"?> 
  2. 02 <?xml version="1.0" encoding="utf-8"?> 
  3. 03 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4. 04 android:layout_width="fill_parent" 
  5. 05 android:layout_height="fill_parent" 
  6. 06 > 
  7. 07 <AnalogClock 
  8. 08 android:id="@+id/aclock" 
  9. 09 android:layout_width="wrap_content" 
  10. 10 android:layout_height="wrap_content" 
  11. 11 android:layout_centerInParent="true" /> 
  12. 12 <DigitalClock 
  13. 13 android:id="@+id/dclock" 
  14. 14 android:layout_width="wrap_content" 
  15. 15 android:layout_height="wrap_content" 
  16. 16 android:layout_below="@id/aclock" 
  17. 17 android:layout_alignLeft="@id/aclock" 
  18. 18 android:layout_marginLeft="40px" /> 
  19. 19 <TextView 
  20. 20 android:layout_width="wrap_content" 
  21. 21 android:layout_height="wrap_content" 
  22. 22 android:text="目前時間:" 
  23. 23 android:layout_toLeftOf="@id/dclock" 
  24. 24 android:layout_alignTop="@id/aclock"/> 
  25. 25 </RelativeLayout>
複製代碼

四、AbsoluteLayout(絕對布局)

絕對布局的子控制項需要指定相對於此座標布局的橫縱座標值,否則將會像架構布局那樣被排在左上方。手機應用需要適應不同的螢幕大小,而這種布局模型不能自適應螢幕尺寸大小,所以應用的相對較少。下面以一個例子簡單說明絕對布局:
<ignore_js_op>

  1. 01 <?xml version="1.0" encoding="utf-8"?> 
  2. 02 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. 03 android:layout_width="fill_parent" 
  4. 04 android:layout_height="fill_parent" 
  5. 05 > 
  6. 06 <TextView 
  7. 07 android:layout_width="wrap_content" 
  8. 08 android:layout_height="wrap_content" 
  9. 09 android:layout_x="10px" 
  10. 10 android:layout_y="10px" android:text="Textview"/> 
  11. 11 <TextView 
  12. 12 android:layout_width="wrap_content" 
  13. 13 android:layout_height="wrap_content" 
  14. 14 android:layout_x="30px" 
  15. 15 android:layout_y="30px" android:text="Textview"/> 
  16. 16 <TextView 
  17. 17 android:layout_width="wrap_content" 
  18. 18 android:layout_height="wrap_content" 
  19. 19 android:layout_x="50px" 
  20. 20 android:layout_y="50px" android:text="Textview"/> 
  21. 21 </AbsoluteLayout>
複製代碼

五、FrameLayout(架構布局)


架構布局是最簡單的布局形式。所有添加到這個布局中的視圖都以層疊的方式顯示。第一個添加的控制項被放在最底層,最後一個添加到架構布局中的視圖顯示在最頂層,上一層的控制項會覆蓋下一層的控制項。這種顯示方式有些類似於堆棧。下面舉一個簡單的例子

<ignore_js_op>

  1. 01 <?xml version="1.0" encoding="utf-8"?> 
  2. 02 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. 03 android:layout_width="fill_parent" android:layout_height="fill_parent"> 
  4. 04 <LinearLayout android:id="@+id/linearLayout1" 
  5. 05 android:layout_height="match_parent" 
  6. 06 android:layout_width="match_parent"> 
  7. 07 <Button android:text="Button" 
  8. 08 android:id="@+id/button1" 
  9. 09 android:layout_width="wrap_content" 
  10. 10 android:layout_height="wrap_content"></Button> 
  11. 11 </LinearLayout> 
  12. 12 <LinearLayout android:layout_width="match_parent" 
  13. 13 android:id="@+id/linearLayout3" 
  14. 14 android:layout_height="match_parent" 
  15. 15 android:gravity="bottom|right"> 
  16. 16 <Button android:text="Button" 
  17. 17 android:id="@+id/button3" 
  18. 18 android:layout_width="wrap_content" 
  19. 19 android:layout_height="wrap_content"></Button> 
  20. 20 </LinearLayout> 
  21. 21 <LinearLayout android:layout_height="match_parent" 
  22. 22 android:id="@+id/linearLayout2" 
  23. 23 android:layout_width="match_parent" 
  24. 24 android:gravity="right"> 
  25. 25 <Button android:text="Button" 
  26. 26 android:id="@+id/button2" 
  27. 27 android:layout_width="wrap_content" 
  28. 28 android:layout_height="wrap_content"></Button> 
  29. 29 </LinearLayout> 
  30. 30 <LinearLayout android:layout_width="match_parent" 
  31. 31 android:id="@+id/LinearLayout01" 
  32. 32 android:layout_height="match_parent" 
  33. 33 android:gravity="bottom|left"> 
  34. 34 <Button android:id="@+id/Button01" 
  35. 35 android:text="Button" 
  36. 36 android:layout_width="wrap_content" 
  37. 37 android:layout_height="wrap_content"></Button> 
  38. 38 </LinearLayout> 
  39. 39 </FrameLayout>
複製代碼

下面介紹一下RelativeLayout用到的一些重要的屬性:

第一類:屬性值為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”
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
android:layout_marginBottom              離某元素底邊緣的距離
android:layout_marginLeft                   離某元素左邊緣的距離
android:layout_marginRight                 離某元素右邊緣的距離
android:layout_marginTop                   離某元素上邊緣的距離

 

摘自:http://bbs.chinaunix.net/thread-3654213-1-1.html

Android開發之詳解五大布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.