歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android SDK Tutorials系列 - Hello Views - Linear Layout

Android SDK Tutorials系列 - Hello Views - Linear Layout

日期:2017/3/1 10:56:39   编辑:Linux編程

LinearLayoutViewGroup 的一種,裡面包含的View按線性方式排列,要麼垂直方向,要麼水平方向。

不要過度使用LinearLayout。如果你開始嵌套使用LinearLayout,那也許你應該考慮使用RelativeLayout了。

  1. 創建一個工程:HelloLinearLayout
  2. 打開 res/layout/main.xml,修改內容如下:
     
    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="fill_parent">
    6. <LinearLayout
    7. android:orientation="horizontal"
    8. android:layout_width="fill_parent"
    9. android:layout_height="fill_parent"
    10. android:layout_weight="1">
    11. <TextView
    12. android:text="red"
    13. android:gravity="center_horizontal"
    14. android:background="#aa0000"
    15. android:layout_width="wrap_content"
    16. android:layout_height="fill_parent"
    17. android:layout_weight="1"/>
    18. <TextView
    19. android:text="green"
    20. android:gravity="center_horizontal"
    21. android:background="#00aa00"
    22. android:layout_width="wrap_content"
    23. android:layout_height="fill_parent"
    24. android:layout_weight="1"/>
    25. <TextView
    26. android:text="blue"
    27. android:gravity="center_horizontal"
    28. android:background="#0000aa"
    29. android:layout_width="wrap_content"
    30. android:layout_height="fill_parent"
    31. android:layout_weight="1"/>
    32. <TextView
    33. android:text="yellow"
    34. android:gravity="center_horizontal"
    35. android:background="#aaaa00"
    36. android:layout_width="wrap_content"
    37. android:layout_height="fill_parent"
    38. android:layout_weight="1"/>
    39. </LinearLayout>
    40. <LinearLayout
    41. android:orientation="vertical"
    42. android:layout_width="fill_parent"
    43. android:layout_height="fill_parent"
    44. android:layout_weight="1">
    45. <TextView
    46. android:text="row one"
    47. android:textSize="15pt"
    48. android:layout_width="fill_parent"
    49. android:layout_height="wrap_content"
    50. android:layout_weight="1"/>
    51. <TextView
    52. android:text="row two"
    53. android:textSize="15pt"
    54. android:layout_width="fill_parent"
    55. android:layout_height="wrap_content"
    56. android:layout_weight="1"/>
    57. <TextView
    58. android:text="row three"
    59. android:textSize="15pt"
    60. android:layout_width="fill_parent"
    61. android:layout_height="wrap_content"
    62. android:layout_weight="1"/>
    63. <TextView
    64. android:text="row four"
    65. android:textSize="15pt"
    66. android:layout_width="fill_parent"
    67. android:layout_height="wrap_content"
    68. android:layout_weight="1"/>
    69. </LinearLayout>
    70. </LinearLayout>

    仔細查看這個XML,最外層是一個LinearLayout,它的android:orientation屬性被設置為"vertical",意味著它包含的View(一共兩個)按照垂直方向排列。它包含的第一個View是另一個LinearLayout,這個LinearLayout使用水平排列方式;包含的第二個LinearLayout使用垂直排列方式。兩個內部的LinearLayout都包含了幾個TextView,這些TextView都按照包含他們的LinearLayout規定的方式排列著。

  3. 現在打開HelloLinearLayout.java,確保它裝載了res/layout/main.xml布局文件,修改如下:

    public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);     setContentView(R.layout.main); }

    setContentView(int) 方法裝載這個Activity的布局文件,資源ID — R.layout.main 指向res/layout/main.xml布局文件。

  4. 運行這個應用。

應該能看到下面的畫面:


注意XML屬性是如何設置每個View的顯示方式的。試著修改android:layout_weight(layout_width/layout_height)的值,看看各個View是如何顯示的。


返回 Android SDK Tutorials系列 - Hello Views
Copyright © Linux教程網 All Rights Reserved