Android入門學習_代碼經常使用佈局

一、線性佈局 LinearLayout:
       線性佈局是全部佈局中最經常使用的類之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls類的父類。LinearLayout可讓它的子元素垂直或水平的方式排成一行(不設置方向的時候默認按照垂直方向排列)。
舉個例子:

java代碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. >
  7. <!--
  8. android:id —— 爲控件指定相應的ID
  9. android:text —— 指定控件當中顯示的文字,須要注意的是,這裏儘可能使用strings.xml文件當中的字符串
  10. android:grivity —— 指定控件的基本位置,好比說居中,居右等位置
  11. android:textSize —— 指定控件當中字體的大小
  12. android:background —— 指定該控件所使用的背景色,RGB命名法
  13. android:width —— 指定控件的寬度
  14. android:height —— 指定控件的高度
  15. android:padding* —— 指定控件的內邊距,也就是說控件當中的內容
  16. android:layout_weight —— 控件之間的權重比
  17. android:sigleLine —— 若是設置爲真的話,則將控件的內容在同一行當中進行顯示
  18. -->
  19. <TextView
  20. android:id="@+id/firstText"
  21. android:text="第一行一行一行一行一行一行一行一行一行一行"
  22. android:gravity="center_vertical"
  23. android:textSize="35pt"
  24. android:background="#aa0000"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:paddingLeft="10dip"
  28. android:paddingTop="20dip"
  29. android:paddingRight="30dip"
  30. android:paddingBottom="40dip"
  31. android:layout_weight="1"
  32. android:singleLine="true"/>
  33. <TextView
  34. android:id="@+id/secondText"
  35. android:text="第二行"
  36. android:gravity="center_vertical"
  37. android:textSize="15pt"
  38. android:background="#0000aa"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"/>
  42. </LinearLayout>
複製代碼

       二、相對佈局 RelativeLayout
       相對佈局 RelativeLayout 容許子元素指定它們相對於其父元素或兄弟元素的位置,這是實際佈局中最經常使用的佈局方式之一。它靈活性大不少,固然屬性也多,操做難度也大,屬性之間產生衝突的的可能性也大,使用相對佈局時要多作些測試。

舉個例子:

java代碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. android:layout_above 將該控件的底部至於給定ID的控件之上
  4. android:layout_below 將該控件的頂部至於給定ID的控件之下
  5. android:layout_toLeftOf 將該控件的右邊緣和給定ID的控件的左邊緣對齊
  6. android:layout_toRightOf 將該控件的左邊緣和給定ID的控件的右邊緣對齊

  7. android:layout_alignBaseline 該控件的baseline和給定ID的控件的baseline對齊
  8. android:layout_alignBottom 將該控件的底部邊緣與給定ID控件的底部邊緣
  9. android:layout_alignLeft 將該控件的左邊緣與給定ID控件的左邊緣對齊
  10. android:layout_alignRight 將該控件的右邊緣與給定ID控件的右邊緣對齊
  11. android:layout_alignTop 將給定控件的頂部邊緣與給定ID控件的頂部對齊


  12. android:alignParentBottom 若是該值爲true,則將該控件的底部和父控件的底部對齊
  13. android:layout_alignParentLeft 若是該值爲true,則將該控件的左邊與父控件的左邊對齊
  14. android:layout_alignParentRight 若是該值爲true,則將該控件的右邊與父控件的右邊對齊
  15. android:layout_alignParentTop 若是該值爲true,則將空間的頂部與父控件的頂部對齊

  16. android:layout_centerHorizontal 若是值爲真,該控件將被至於水平方向的中央
  17. android:layout_centerInParent 若是值爲真,該控件將被至於父控件水平方向和垂直方向的中央
  18. android:layout_centerVertical 若是值爲真,該控件將被至於垂直方向的中央
  19. -->
  20. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent">
  23. <TextView
  24. android:id="@+id/label"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="Type here:" />
  28. <EditText
  29. android:id="@+id/entry"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:background="@android:drawable/editbox_background"
  33. android:layout_below="@id/label" />
  34. <Button android:id="@+id/ok"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@id/entry"
  38. android:layout_alignParentRight="true"
  39. android:layout_marginLeft="10dip"
  40. android:text="OK" />
  41. <Button android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_toLeftOf="@id/ok"
  44. android:layout_alignTop="@id/ok"
  45. android:text="Cancel" />
  46. </RelativeLayout>
複製代碼

       三、表單佈局 TableLayout
       和TableRow配合使用,和HTML裏的Table類似。

舉個例子:

java代碼:
  1. <?xml version="1.0" encoding="utf-8" ?>

  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"android:layout_height="fill_parent" android:stretchColumns="1">
  3. <TableRow>

  4. <TextView
  5. android:layout_column="1"
  6. android:text="打開..."
  7. android:padding="3dip" />

  8. <TextView
  9. android:text="Ctrl-O"
  10. android:gravity="right"
  11. android:padding="3dip" />
  12. </TableRow>
  13. <TableRow>


  14. <TextView
  15. android:layout_column="1"
  16. android:text="保存..."
  17. android:padding="3dip" />

  18. <TextView
  19. android:text="Ctrl-S"
  20. android:gravity="right"
  21. android:padding="3dip" />
  22. </TableRow>

  23. <TableRow>
  24. <TextView
  25. android:layout_column="1"
  26. android:text="另存爲..."
  27. android:padding="3dip" />

  28. <TextView
  29. android:text="Ctrl-Shift-S"
  30. android:gravity="right"
  31. android:padding="3dip" />
  32. </TableRow>

  33. <View
  34. android:layout_height="2dip"
  35. android:background="#FF909090" />
  36. <TableRow>
  37. <TextView android:text="*" android:padding="3dip" />
  38. <TextView android:text="導入..." android:padding="3dip" />
  39. </TableRow>
  40. <TableRow>
  41. <TextView android:text="*" android:padding="3dip" />
  42. <TextView android:text="導出..." android:padding="3dip" />
  43. <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />
  44. </TableRow>

  45. <View android:layout_height="2dip" android:background="#FF909090" />
  46. <TableRow>
  47. <TextView android:layout_column="1" android:text="退出" android:padding="3dip" />

  48. </TableRow>

  49. </TableLayout>
複製代碼

       四、切換卡 Tabwidget
       繼承TabActivity,實現標籤的切換功能。
舉個例子:

java代碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent">
  18. <TextView
  19. android:id="@+id/textview1"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:text="this is a tab" />
  23. <TextView
  24. android:id="@+id/textview2"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:text="this is another tab" />
  28. <TextView
  29. android:id="@+id/textview3"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="this is a third tab" />
  33. </FrameLayout>
  34. </LinearLayout>
  35. </TabHost>
複製代碼

       其餘佈局:java


       一、幀佈局 FrameLayout:android

       是最簡單的一個佈局對象。在他裏面的的全部顯示對象愛你過都將固定在屏幕的左上角,不能指定位置,但容許有多個顯示對象,只是後一個會直接覆蓋在前一個之上顯示,會把前面的組件部分或所有擋住。佈局

舉個例子:測試


java代碼:
字體

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:text="big"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="50pt"/>
  11. <TextView
  12. android:text="middle"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:textSize="20pt"/>
  16. <TextView
  17. android:text="small"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textSize="10pt"/>
  21. </FrameLayout>
複製代碼

       二、絕對佈局 AbsoluteLayoutthis

       絕對定位AbsoluteLayout,又能夠叫作座標佈局,能夠直接指定子元素的絕對位置,這種佈局簡單直接,直觀性強,可是因爲手機屏幕尺寸差異比較大,使用絕對定位的適應性會比較差。分辨率不同的屏幕,顯示的位置也會有所不一樣。spa

舉個例子:3d


java代碼:
code

  1. < ?xml version="1.0" encoding="utf-8"?>
  2. < AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >

  7. < EditText
  8. android:text="Welcome to Mr Wei's blog"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"/>

  11. < Button
  12. android:layout_x="250px" //設置按鈕的X座標
  13. android:layout_y="40px" //設置按鈕的Y座標
  14. android:layout_width="70px" //設置按鈕的寬度
  15. android:layout_height="wrap_content"
  16. android:text="Button"/>

  17. < /AbsoluteLayout>
複製代碼
相關文章
相關標籤/搜索