1. 程式人生 > >Android Layout 佈局屬性全解

Android Layout 佈局屬性全解

Android  Layout 佈局屬性全解二

<LinearLayout>

  1. <LinearLayout
  2. android:orientation="horizontal/vertical"   //水平或豎直線性佈局   
  3. android:baselineAligned="true"              //見下  
  4. android:baselineAlignedChildIndex=""        //以指定子元件作為基線對齊  
  5. android:weightSum=""                        //“定義weight總和的最大值。如果未指定該值,以所有子檢視的layout_weight屬性的累加值作為總和的最大值。一個典型的案例是:通過指定子檢視的layout_weight屬性為0.5,並設定LinearLayout的weightSum屬性為1.0,實現子檢視佔據可用寬度的50%。”  
  6. 設想一個場景:我們要在盒子裡放置其他物體。盒子可用空間的比例就是weightSum,盒子中每個物體可用空間的比例就是layout_weight。例如,盒子的WeightSum是1,我們需要往盒子裡放置兩個物體:物體A和物體B。物體A的layout_weight為0.25,物體B的layout_weight為0.75。那麼,物體A可以佔據盒子25%的空間,而物體B可以佔據剩下的75%的空間。  
  7. android:divider=""
  8. android:dividerPadding=""
  9. android:showDividers="">

 Android官方文件是這麼描述的:

     那到底這個屬性是做什麼用的呢?

         baselineAligned:基準線對齊。

     首先要解釋什麼是基準線,這個在中文中不常見,但在以字母為書寫語言的其他國家非常常見。

      如上圖所示,紅線就是基線(baseline),是不是很熟悉,這不就是我們經常寫英文的四條線中的第三條嗎。

     那baselineAligned是做什麼用的呢?根據官方文件,baselineAligned預設設定為true,當設定為false時,

      佈局檔案和它的子元件的基準線不對齊。

      讓我們通過一個例子來看看效果怎樣的。

複製程式碼
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:baselineAligned="false"
 6     android:orientation="horizontal">
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_marginRight="5dip"
12         android:text="TextView:p" />
13     <TextView 
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_marginRight="5dip"
17         android:textSize="30sp"
18         android:text="LargeTextView:p"
19         />
20 </LinearLayout>
複製程式碼

 這是將baselineAligned值設定為false時,也就是不對齊。看看執行效果:

  把baselineAligned值改為true。

  看,差別明顯,這樣就很好的理解了baselineAligned的作用了。其實,這個並不難,但我覺得不管難不難,只要不明白,就應該多思考,想想為什麼。

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

API說明

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或者是containerlayout_weight及該權值在所有widgets或者是containers中的比例。例如,如果有三個文字框,其中兩個指定的權值為1,那麼,這兩個文字框將等比例地放大,並填滿剩餘的空間,而第三個文字框不會放大,按實際大小來顯示。如果前兩個文字框的取值一個為2,一個為1,顯示第三個文字框後剩餘的空間的2/3給權值為2的,1/3大小給權值為1的。也就是權值越大,重要度越大。

如果LinearLayout包含子LinearLayout,子LinearLayout之間的權值越大的,重要度則越小。如果有LinearLayout A包含LinearLayout C,DC的權值為2D的權值為1,則螢幕的2/3空間分給權值為1D1/3分給權值為2C。在LinearLayout巢狀的情況下,子LinearLayout必須要設定權值,否則預設的情況是未設定權值的子LinearLayout佔據整個螢幕。


Android Layout佈局屬性三

<RelativeLayout>

  1. android:layout_above="@id/xxx"  --將控制元件置於給定ID控制元件之上  
  2. android:layout_below="@id/xxx"  --將控制元件置於給定ID控制元件之下  
  3. android:layout_toLeftOf="@id/xxx"  --將控制元件的右邊緣和給定ID控制元件的左邊緣對齊  
  4. android:layout_toRightOf="@id/xxx"  --將控制元件的左邊緣和給定ID控制元件的右邊緣對齊  
  5. android:layout_alignLeft="@id/xxx"  --將控制元件的左邊緣和給定ID控制元件的左邊緣對齊  
  6. android:layout_alignTop="@id/xxx"  --將控制元件的上邊緣和給定ID控制元件的上邊緣對齊  
  7. android:layout_alignRight="@id/xxx"  --將控制元件的右邊緣和給定ID控制元件的右邊緣對齊  
  8. android:layout_alignBottom="@id/xxx"  --將控制元件的底邊緣和給定ID控制元件的底邊緣對齊  
  9. android:layout_alignParentLeft="true"  --將控制元件的左邊緣和父控制元件的左邊緣對齊  
  10. android:layout_alignParentTop="true"  --將控制元件的上邊緣和父控制元件的上邊緣對齊  
  11. android:layout_alignParentRight="true"  --將控制元件的右邊緣和父控制元件的右邊緣對齊  
  12. android:layout_alignParentBottom="true" --將控制元件的底邊緣和父控制元件的底邊緣對齊  
  13. android:layout_centerInParent="true"  --將控制元件置於父控制元件的中心位置  
  14. android:layout_centerHorizontal="true"  --將控制元件置於水平方向的中心位置  
  15. android:layout_centerVertical="true"  --將控制元件置於垂直方向的中心位置  

http://blog.csdn.net/jaysong2012/article/details/41079227
http://blog.csdn.net/jaysong2012/article/details/41079227
http://blog.csdn.net/jaysong2012/article/details/41079227

View屬性說明

下面對View的屬性進行詳細說明


關聯方法: setAlpha(float) 
屬性說明: 檢視透明度,值在0-1之間。0為完全透明,1為完全不透明。

android:background
關聯方法: setBackgroundResource(int) 
屬性說明: 檢視背景

android:clickable
關聯方法: setClickable(boolean) 
屬性說明: 檢視是否可點選

android:contentDescription
關聯方法: setContentDescription(CharSequence) 
屬性說明: 設定View的備註說明,作為一種輔助功能提供,為一些沒有文字描述的View提供說明

android:drawingCacheQuality
關聯方法: setDrawingCacheQuality(int) 
屬性說明: "設定繪圖時半透明質量。有可以取以下3個值 auto——預設,由框架決定 high——高質量,使用較高的顏色深度,消耗更多的記憶體 low——低質量,使用較低的顏色深度,但是用更少的記憶體"

android:duplicateParentState
關聯方法: 
屬性說明: 如果設定此屬性,將直接從父容器中獲取繪圖狀態(游標,按下等)

android:fadeScrollbars
關聯方法: setScrollbarFadingEnabled(boolean) 
屬性說明: 定義在ScrollBar沒有使用時,是否褪色。

android:fadingEdgeLength
關聯方法: getVerticalFadingEdgeLength() 
屬性說明: 設定邊框漸變的長度。

android:filterTouchesWhenObscured
關聯方法: setFilterTouchesWhenObscured(boolean) 
屬性說明: view所在視窗被其它可見視窗遮住時,是否過濾觸控事件。

android:fitsSystemWindows
關聯方法: setFitsSystemWindows(boolean) 
屬性說明: 設定佈局調整時是否考慮系統視窗(如狀態列)

android:focusable
關聯方法: setFocusable(boolean) 
屬性說明: 設定是否獲得焦點。若有requestFocus()被呼叫時,後者優先處理。注意在表單中想設定某一個如EditText獲取焦點,光設定這個是不行的,需要將這個EditText前面的focusable都設定為false才行。在Touch模式下獲取焦點需要設定focusableInTouchMode為true。

android:focusableInTouchMode
關聯方法: setFocusableInTouchMode(boolean) 
屬性說明: 設定在Touch模式下View是否能取得焦點。

android:hapticFeedbackEnabled
關聯方法: setHapticFeedbackEnabled(boolean) 
屬性說明: 是否啟用觸控反饋,啟用後就是在點選等操作時會有震動等反饋效果

android:id
關聯方法: setId(int) 
屬性說明: 給當前View設定一個在當前layout.xml中的唯一編號,可以通過呼叫View.findViewById() 或Activity.findViewById()根據這個編號查詢到對應的View。不同的layout.xml之間定義相同的id不會衝突。

android:importantForAccessibility
關聯方法: setImportantForAccessibility(int) 
屬性說明: 設定可達性的重要性

android:isScrollContainer
關聯方法: setScrollContainer(boolean) 
屬性說明: 設定當前View為滾動容器。這裡沒有測試出效果來,ListView/ GridView/ ScrollView根本就不用設定這個屬性,而EdidText設定android:scrollbars也能出滾動條

android:keepScreenOn
關聯方法: setKeepScreenOn(boolean) 
屬性說明: 檢視在可見的情況下是否保持喚醒狀態。

android:layerType
關聯方法: setLayerType(int,Paint) 
屬性說明: "設定指定層的型別,可以取以下3個值: none——不指定 software——軟體層。 hardware——硬體層。使用硬體加速。"

android:layoutDirection
關聯方法: setLayoutDirection(int) 
屬性說明: 定義佈局圖紙的方向

android:longClickable
關聯方法: setLongClickable(boolean) 
屬性說明: 是否響應長點選事件

android:minHeight
關聯方法: setMinimumHeight(int) 
屬性說明: 設定檢視最小高度

android:minWidth
關聯方法: setMinimumWidth(int) 
屬性說明: 設定檢視最小寬度

android:nextFocusDown
關聯方法: setNextFocusDownId(int) 
屬性說明: 向下移動焦點時,下一個獲取焦點的view的id

android:nextFocusForward
關聯方法: setNextFocusForwardId(int) 
屬性說明: 下一個獲取焦點的view的id

android:nextFocusLeft
關聯方法: setNextFocusLeftId(int) 
屬性說明: 向左移動焦點時,下一個獲取焦點的view的id

android:nextFocusRight
關聯方法: setNextFocusRightId(int) 
屬性說明: 向右移動焦點時,下一個獲取焦點的view的id

android:nextFocusUp
關聯方法: setNextFocusUpId(int) 
屬性說明: 向上移動焦點時,下一個獲取焦點的view的id

android:onClick
關聯方法: 
屬性說明: 點選時,要呼叫的方法的名稱。

android:padding
關聯方法: setPaddingRelative(int,int,int,int) 
屬性說明: 設定上下左右的邊距

android:paddingBottom
關聯方法: setPaddingRelative(int,int,int,int) 
屬性說明: 下邊距

android:paddingEnd
關聯方法: setPaddingRelative(int,int,int,int) 
屬性說明: 與android:paddingRight相同

android:paddingLeft
關聯方法: setPadding(int,int,int,int) 
屬性說明: 左邊距

android:paddingRight
關聯方法: setPadding(int,int,int,int) 
屬性說明: 右邊距

android:paddingStart
關聯方法: setPaddingRelative(int,int,int,int) 
屬性說明: android:paddingLeft相同

android:paddingTop
關聯方法: setPaddingRelative(int,int,int,int) 
屬性說明: 上邊距

android:requiresFadingEdge
關聯方法: setVerticalFadingEdgeEnabled(boolean) 
屬性說明: 定義滾動時邊緣是否褪色

android:rotation
關聯方法: setRotation(float) 
屬性說明: 旋轉度數

android:rotationX
關聯方法: setRotationX(float) 
屬性說明: 水平旋轉度數

android:rotationY
關聯方法: setRotationY(float) 
屬性說明: 豎直旋轉度數

android:saveEnabled
關聯方法: setSaveEnabled(boolean) 
屬性說明: 在配置改變等情況出現時是否儲存view的狀態資料。如果你的view有id,那預設系統就會幫你儲存。

android:scaleX
關聯方法: setScaleX(float) 
屬性說明: 水平方向縮放比例

android:scaleY
關聯方法: setScaleY(float) 
屬性說明: 豎直方向縮放比例

android:scrollX
關聯方法: 
屬性說明: x方向的滾動偏移。即在水平方向滾動了多少距離

android:scrollY
關聯方法: 
屬性說明: y方向的滾動偏移。即在豎直方向滾動了多少距離

android:scrollbarAlwaysDrawHorizontalTrack
關聯方法: 
屬性說明: 是否總是繪製水平滾動條的滾動軌道

android:scrollbarAlwaysDrawVerticalTrack
關聯方法: 
屬性說明: 是否總是繪製豎直滾動條的滾動軌道

android:scrollbarDefaultDelayBeforeFade
關聯方法: setScrollBarDefaultDelayBeforeFade(int) 
屬性說明: 滾動條在n毫秒後開始淡出。

android:scrollbarFadeDuration
關聯方法: setScrollBarFadeDuration(int) 
屬性說明: 滾動條用多長時間淡出完畢。

android:scrollbarSize
關聯方法: setScrollBarSize(int) 
屬性說明: 設定滾動條的尺寸。垂直滾動條的寬度、水平滾動條的高度

android:scrollbarStyle
關聯方法: setScrollBarStyle(int) 
屬性說明: "滾動條的風格。共4組值: insideOverlay——內貼圖 insideInset——內插圖 outsideOverlay——外貼圖 outsideInset——外插圖。 inside就是滾動條在繪製在padding以內;outside就是不需要繪製在padding內(即view的邊界處);Overlay是貼圖,就是直接覆蓋在內容的上方,這樣內容可能會顯示到滾動條下方去;Inset是插圖,就是會在對應padding上加上滾動條的寬度,以不讓內容顯示到滾動條下面去。"

android:scrollbarThumbHorizontal
關聯方法: 
屬性說明: 水平滾動塊的drawable物件

android:scrollbarThumbVertical
關聯方法: 
屬性說明: 豎直滾動塊的drawable物件

android:scrollbarTrackHorizontal
關聯方法: 
屬性說明: 水平滾動條滾動軌道的drawable物件

android:scrollbarTrackVertical
關聯方法: 
屬性說明: 豎直滾動條滾動軌道的drawable物件

android:scrollbars
關聯方法: 
屬性說明: "設定可顯示的滾動條。有3個取值: none——不顯示滾動條 horizontal——顯示水平滾動條 vertical——顯示豎直滾動條"

android:soundEffectsEnabled
關聯方法: setSoundEffectsEnabled(boolean) 
屬性說明: 點選或觸控該view時,是否需要有聲音效果

android:tag
關聯方法: 
屬性說明: string標識。類似id,id是整數標識。

android:textAlignment
關聯方法: setTextAlignment(int) 
屬性說明: 設定文字的顯示方式。

android:textDirection
關聯方法: setTextDirection(int) 
屬性說明: 設定文字的顯示方向。

android:transformPivotX
關聯方法: setPivotX(float) 
屬性說明: 水平方向偏轉量

android:transformPivotY
關聯方法: setPivotY(float) 
屬性說明: 豎直方向偏轉量

android:translationX
關聯方法: setTranslationX(float) 
屬性說明: 水平方向的移動距離

android:translationY
關聯方法: setTranslationY(float) 
屬性說明: 豎直方向的移動距離

android:visibility
關聯方法: setVisibility(int) 
屬性說明: "view的可見性。有3個取值: gone——不可見,同時不佔用view的空間; invisible——不可見,但佔用view的空間; visible—