android LinearLayout

LinearLayout是線性佈局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列全部的widgets或者其餘的containers,超過邊界時,某些控件將缺失或消失。所以一個垂直列表的每一行只會有一個widget或者是container,而無論他們有多寬,而一個水平列表將會只有一個行高(高度爲最高子控件的高度加上邊框高度)。LinearLayout保持其所包含的widget或者是container之間的間隔以及互相對齊(相對一個控件的右對齊、中間對齊或者左對齊)。android

xml屬性

android:baselineAligned;//是否容許用戶調整它內容的基線。

android:baselineAlignedChildIndex;//當一個線性佈局與另外一個佈局是按基線對齊的一部分,它能夠指定其內容的基線對齊方式。

android:gravity;//指定如何在該對象中放置此對象的內容(x/y座標值)。

android:orientation:設置它內容的對其方向(橫向/豎向)。

LinearLayout還支持爲其包含的widget或者是container指定填充權值。好處就是容許其包含的widget或者是container能夠填充屏幕上的剩餘空間。這也避免了在一個大屏幕中,一串widgets或者是containers擠成一堆的狀況,而是容許他們放大填充空白。剩餘的空間會按這些widgets或者是containers指定的權值比例分配屏幕。默認的 weight 值爲0,表示按照widgets或者是containers實際大小來顯示,若高於0的值,則將Container剩餘可用空間分割,分割大小具體取決於每個widget或者是container的layout_weight及該權值在全部widgets或者是containers中的比例。例如,若是有三個文本框,其中兩個指定的權值爲1,那麼,這兩個文本框將等比例地放大,並填滿剩餘的空間,而第三個文本框不會放大,按實際大小來顯示。若是前兩個文本框的取值一個爲2,一個爲1,顯示第三個文本框後剩餘的空間的2/3給權值爲2的,1/3大小給權值爲1的。也就是權值越大,重要度越大。

若是LinearLayout包含子LinearLayout,子LinearLayout之間的權值越大的,重要度則越小。若是有LinearLayout A包含LinearLayout C,D,C的權值爲2,D的權值爲1,則屏幕的2/3空間分給權值爲1的D,1/3分給權值爲2的C。在LinearLayout嵌套的狀況下,子LinearLayout必需要設置權值,不然默認的狀況是未設置權值的子LinearLayout佔據整個屏幕。

<?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",其餘屬性不變

運行效果:

相關文章
相關標籤/搜索