LinearLayout是線性佈局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列全部的widgets或者其餘的containers,超過邊界時,某些控件將缺失或消失。所以一個垂直列表的每一行只會有一個widget或者是container,而無論他們有多寬,而一個水平列表將會只有一個行高(高度爲最高子控件的高度加上邊框高度)。LinearLayout保持其所包含的widget或者是container之間的間隔以及互相對齊(相對一個控件的右對齊、中間對齊或者左對齊)。android
xml屬性 android:baselineAligned;//是否容許用戶調整它內容的基線。 android:baselineAlignedChildIndex;//當一個線性佈局與另外一個佈局是按基線對齊的一部分,它能夠指定其內容的基線對齊方式。 android:gravity;//指定如何在該對象中放置此對象的內容(x/y座標值)。 android:orientation:設置它內容的對其方向(橫向/豎向)。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal"> <!-- android:orientation="vertical" 表示豎直方式對齊 android:orientation="horizontal"表示水平方式對齊 android:layout_width="fill_parent"定義當前視圖在屏幕上 能夠消費的寬度,fill_parent即填充整個屏幕。 android:layout_height="wrap_content":隨着文字欄位的不一樣 而改變這個視圖的寬度或者高度。有點自動設置框度或者高度的意思 layout_weight 用於給一個線性佈局中的諸多視圖的重要度賦值。 全部的視圖都有一個layout_weight值,默認爲零,意思是須要顯示 多大的視圖就佔據多大的屏幕空 間。若賦一個高於零的值,則將父視 圖中的可用空間分割,分割大小具體取決於每個視圖的 layout_weight 值以及該值在當前屏幕布局的總體 layout_weight值和在其它視圖屏幕布 局的layout_weight值中所佔的比率而定。 舉個例子:好比說咱們在 水平方向上有一個文本標籤和兩個文本編輯元素。 該文本標籤並沒有指定layout_weight值,因此它將佔據須要提供的最少空間。 若是兩個文本編輯元素每個的layout_weight值都設置爲1,則二者平分 在父視圖佈局剩餘的寬度(由於咱們聲明這二者的重要度相等)。若是兩個 文本編輯元素其中第一個的layout_weight值設置爲1,而第二個的設置爲2, 則剩餘空間的三分之二分給第一個,三分之一分給第二個(數值越小,重要 度越高)。 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BB" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BB" /> </LinearLayout>
須要注意的是LinearLayout佈局不會換行:當組件排到容器盡頭時,其他的組件將不會被顯示。佈局
先明確幾個概念的區別:
padding margin都是邊距的含義,關鍵問題得明白是什麼相對什麼的邊距.
padding是控件的內容相對控件的邊緣的邊距.
margin是控件邊緣相對父空間的邊距. 測試
android:gravity 屬性是對該view 內容的限定.好比一個button 上面的text. 你能夠設置該text 在view的靠左,靠右等位置.該屬性就幹了這個.
android:layout_gravity是用來設置該view中的子view相對於父view的位置.好比一個button 在linearlayout裏,你想把該button放在靠左,靠右等位置就能夠在linearlayout中經過該屬性設置. spa
二:屬性效果:code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/custom_titlebar" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/imageViewLoginState" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="1"> </Button> <Button android:id="@+id/imageViewLoginState1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:text="2" > </Button> </LinearLayout>
二:接下來看一下 android:layout_width屬性:xml
這裏測試的是垂直佈局android:orientation="vertical",因此針對組件的 android:layout_height屬性;若是測試的水平佈局,則需針對組件的 android:layout_width屬性。 對象
1. android:layout_height="fill_parent"utf-8
設置 :TextView_one屬性android:layout_weight="1"get
TextView_Two屬性android:layout_weight="3"it
效果:
接下來咱們只是設置一下1. android:layout_height="wrap_content",其餘屬性不變
運行效果: