Android布局管理詳解(1)—— LinearLayout 線性布局


  Android的布局方式共有6種,分別是LinearLayout(線性布局)TableLayout(表格布局)FrameLayout(幀布局)RelativeLayout(相對布局)GridLayout(網格布局)以及AbsoluteLayout(絕對布局)

  本次主要介紹 LinearLayout 線性布局,其余布局留待以后介紹。

  線性布局由 LinearLayout 類來代表, LinearLayout 可以控制各組件橫向或縱向排列。

  LinearLayout 常用屬性介紹

  1.  android:orientation

    設置布局管理器內組件的排列方式,可設置為 horizon (水平排列)、vertical (垂直排列)。

    

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <Button
 7         android:id="@+id/btn1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="@string/btn1"/>
11     <Button
12         android:id="@+id/btn2"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/btn2"/>
16     <Button
17         android:id="@+id/btn3"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="@string/btn3"/>
21 </LinearLayout>

  上述代碼設置了三個按鈕(Button),第 3 行代碼設置其排列方式為 horizon 水平排列,效果圖如圖 1: 

圖 1 :                                                   圖 2 :                                                圖 3 :

                              

 將上述代碼的第 3 行中 orientation 的值由 horizon 改為 vertical (如下代碼),則按鈕的排列方式變為垂直排列 ,如圖 2

 

 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="match_parent"
 5     android:layout_height="match_parent">
 6     <Button
 7         android:id="@+id/btn1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="@string/btn1"/>
11     <Button
12         android:id="@+id/btn2"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/btn2"/>
16     <Button
17         android:id="@+id/btn3"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="@string/btn3"/>
21 </LinearLayout>
View Code

   注意: Android 的線性布局不會換行,當組件一個挨着一個的排列到頭之后,剩下的組件將不會被顯示出來

  例:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <Button
 7         android:id="@+id/btn1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="@string/btn1"/>
11     <Button
12         android:id="@+id/btn2"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/btn2"/>
16     <Button
17         android:id="@+id/btn3"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="@string/btn3"/>
21     <Button
22         android:id="@+id/btn4"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:text="@string/btn4"/>
26     <Button
27         android:id="@+id/btn5"
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:text="@string/btn5"/>
31 </LinearLayout>
View Code

上述代碼定義了 5 個按鈕,使其水平排列,但其效果如上圖 3 所示,只顯示了四個按鈕,第 5 個按鈕沒有被顯示出來。

   2.  android:gravity

    設置布局管理器內組件的對齊方式,該屬性值可設為 top(頂部對齊) 、bottom(底部對齊) 、left(左對齊) 、right(右對齊) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直與水平方向都居中) 、 fill (填充)、  clip_vertical(垂直方向裁剪) 、  clip_horizontal(水平方向裁剪) 。

    可同時指定多種對其方式的組合,中間用“|”連接,如下方代碼設置對齊方式為 left|center_vertical 表示出現在屏幕左邊且垂直居中,效果圖如圖 4 。

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:gravity="left|center_vertical">
 7     <Button
 8         android:id="@+id/btn1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="@string/btn1"/>
12     <Button
13         android:id="@+id/btn2"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="@string/btn2"/>
17     <Button
18         android:id="@+id/btn3"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="@string/btn3"/>
22 </LinearLayout>
View Code

   圖 4 :

    

除以上兩個常用屬性外,LinearLayout 的屬性還有以下幾個:

  android:baselineAligned            該屬性設為 false ,將會阻止該布局管理器與它的子元素的基線對其。

  android:divider                設置垂直布局時兩個按鈕直接的分隔條。

  android:measureWithLargestChild      該屬性設為 true 時,所有帶權重的子元素都會具有最大子元素的最小尺寸。

最后介紹一下 LinearLayout 子元素支持的常用屬性:

  android:layout_gravity    指定該子元素在 LinearLayout 中的對其方式

  android:layout_weight    指定該子元素在 LinearLayout 中所占的權重

以上就是關於 android 布局中 LinearLayout 的介紹,如有疏漏或錯誤,歡迎指正。

  本文為博主原創文章,轉載請在明顯位置注明出處:

  原文地址 http://www.cnblogs.com/zzulihao


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM