爲了更好的管理Android應用的用戶界面中的組件,Android提供了佈局管理器。經過使用佈局管理器,Android應用的圖形用戶界面具備良好的平臺無關性。一般,推薦使用佈局管理器來管理組件的分佈、大小,而不是直接設置組件位置和大小。例如,當設置一個文本框(TextView),爲了讓這個文本框在不一樣的手機屏幕上都能良好的運行,手動的控制它的大小及位置將給編程帶來巨大的困難,而使用佈局管理器則能夠解決這個問題,佈局管理器能夠根據運行平臺來調整組件的大小,而程序員須要作的,僅僅是爲容器選擇合適的佈局管理器。android
Android的佈局管理器自己也是一個UI組件,Android中的全部佈局管理器都是ViewGroup的子類。全部的佈局管理器均可以做爲容器類使用,所以能夠調用多個重載的addView()向佈局管理器中添加組件,咱們也能夠在一個佈局管理器中嵌套其餘的佈局管理器,由於佈局管理器也繼承了View,也可做爲普通的UI組件使用。程序員
**GridLayout和RelativeLayout已被Android9標註爲不推薦使用,推薦使用ConstraintLayout替代它們,編程
⒈LinearLayout(線性佈局)app
線性佈局由LinearLayout類來表明,線性佈局有點像Swing編程裏的Box,它們都會將容器裏的組件一個挨着一個排列起來。LinearLayout能夠控制各組件時橫向排列仍是縱向排列(經過屬性android:orientation控制)。ide
Android的線性佈局不會換行,當組件一個挨着一個排列到頭以後,剩下的組件將不會顯示出來。佈局
LinearLayout經常使用的XML屬性及相關方法以下表:spa
XML屬性 | 相關方法 | 說明 |
android:baselineAligned | setBaselineAligned(boolean) | 該屬性設置爲false,將會阻止該佈局管理器與code 它的子元素的基線對齊xml |
android:divider | setDividerDrawable(Drawable) | 設置垂直佈局時兩個按鈕之間的分割線 |
android:gravity | setGravity(int) | 設置佈局管理器內組件的對齊方式。該屬性blog 支持top、bottom、left、right、center_vertical、 fill_vertical、center_horizontal、fill_horizontal、 center、fill、clip_vertical、clip_horizontal幾個屬性值。 也能夠同時指定多種對齊方式的組合。例如left|center_vertical 表明出如今屏幕左邊並且垂直居中。 |
android:measureWithLargestChild | setMeasureWithLargestChildEnabled(boolean) | 當該屬性設置爲true時,全部帶權重的子元素都會具備 最大子元素的最小尺寸 |
android:orientation | setOrientation(int) | 設置佈局管理器內組件的排列方式,能夠設置爲horizontal (水平排列)、vertical(垂直排列,默認值)兩個值的 其中之一 |
android:weightSum | 設置該佈局管理器的最大權值和 |
LinearLayout包含的全部子元素都受LinearLayout.LayoutParams控制,所以LinearLayout包含的子元素能夠額外指定以下屬性。
XML屬性 | 相關方法 | 說明 |
android:layout_gravity | 指定該子元素在LinearLayout中的對齊方式 | |
android:layout_weight | 指定該子元素在LinearLayout中所佔的權重 |
**基本上不少佈局管理器都提供了相應的LayoutParams內部類,該內部類用於控制它們的子元素支持指定android:layout_gravity屬性,該屬性設置該子元素在父容器中的對齊方式。與android:layout_gravity類似的屬性還有android:gravity屬性(通常容器才支持指定該屬性),該屬性用於控制它所包含的子元素的對齊方式。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="bottom|center_horizontal" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> </LinearLayout>
⒉TableLayout(表格佈局)