Layouts介紹


Android系統佈局的介紹,將簡單介紹一下RelativeLayout、LinearLayout、FrameLayout、Constraintlayout、TableLayout、AbsoluteLayout、GridLayoutandroid

目錄

-LinearLayout緩存

-RelativeLayoutapp

-FrameLayoutide

-Constraintlayout佈局

-TableLayout優化

-AbsoluteLayout動畫

-GridLayoutui

-通用屬性spa

LinearLayout

線性佈局在開發中使用居多,具備垂直方向與水平方向的佈局方式,經過設置屬性「android:orientation」控制方向,屬性值垂直(vertical)和水平(horizontal),默認水平方向。設計

相關經常使用屬性:

android:gravity:內部控件對齊方式,經常使用屬性值有center、center_vertical、center_horizontal、top、bottom、left、right等。

android:layout_weight:權重,用來分配當前控件在剩餘空間的大小。使用權重通常要把分配該權重方向的長度設置爲零,好比在水平方向分配權重,就把width設置爲零。️weightSum權重總值⚠️用android:layout_weight,會致使佈局從新繪製,非特殊狀況通常不使用

android:layout_gravity:控制子組件在夫容器裏的對其方式

RelativeLayout

相對佈局可讓子控件相對於兄弟控件或父控件進行佈局,能夠設置子控件相對於兄弟控件或父控件進行上下左右對齊。RelativeLayout能替換一些嵌套視圖,當咱們用LinearLayout來實現一個簡單的佈局但又使用了過多的嵌套時,就能夠考慮使用RelativeLayout從新佈局。相對佈局就是必定要加Id才能管理。

經常使用屬性: 相對於父控件: 例子:android:layout_alignParentTop=「true」 android:layout_alignParentTop 控件的頂部與父控件的頂部對齊; android:layout_alignParentBottom 控件的底部與父控件的底部對齊; android:layout_alignParentLeft 控件的左部與父控件的左部對齊; android:layout_alignParentRight 控件的右部與父控件的右部對齊;

相對給定Id控件: 例子:android:layout_above=「@id/**」 android:layout_above 控件的底部置於給定ID的控件之上; android:layout_below 控件的底部置於給定ID的控件之下; android:layout_toLeftOf 控件的右邊緣與給定ID的控件左邊緣對齊; android:layout_toRightOf 控件的左邊緣與給定ID的控件右邊緣對齊; android:layout_alignBaseline 控件的baseline與給定ID的baseline對齊; android:layout_alignTop 控件的頂部邊緣與給定ID的頂部邊緣對齊; android:layout_alignBottom 控件的底部邊緣與給定ID的底部邊緣對齊; android:layout_alignLeft 控件的左邊緣與給定ID的左邊緣對齊; android:layout_alignRight 控件的右邊緣與給定ID的右邊緣對齊;

居中方式: 例子:android:layout_centerInParent=「true」 android:layout_centerHorizontal 水平居中; android:layout_centerVertical 垂直居中; android:layout_centerInParent 父控件的中央;

FrameLayout

幀佈局,從屏幕左上角按照層次堆疊方式佈局,後面的控件覆蓋前面的控件。例子:設計地圖

經常使用屬性:

android:foreground:設置改幀佈局容器的前景圖像

android:foregroundGravity:設置前景圖像顯示的位置

Constraintlayout

約束佈局ConstraintLayout 是一個ViewGroup,能夠在Api9以上的Android系統使用它,它的出現主要是爲了解決佈局嵌套過多的問題,以靈活的方式定位和調整小部件。從 Android Studio 2.3 起,官方的模板默認使用 ConstraintLayout。

經常使用屬性: 相對定位: app:layout_constraintLeft_toLeftOf app:layout_constraintLeft_toRightOf app:layout_constraintRight_toLeftOf app:layout_constraintRight_toRightOf app:layout_constraintTop_toTopOf app:layout_constraintTop_toBottomOf app:layout_constraintBottom_toTopOf app:layout_constraintBottom_toBottomOf app:layout_constraintBaseline_toBaselineOf app:layout_constraintStart_toEndOf app:layout_constraintStart_toStartOf app:layout_constraintEnd_toStartOf app:layout_constraintEnd_toEndOf

角度定位: app:layout_constraintCircle="@+id/TextView1" app:layout_constraintCircleAngle="120"(角度) app:layout_constraintCircleRadius="150dp"(距離)

邊距: android:layout_marginStart android:layout_marginEnd android:layout_marginLeft android:layout_marginTop android:layout_marginRight android:layout_marginBottom app:layout_goneMarginStart app:layout_goneMarginEnd app:layout_goneMarginLeft app:layout_goneMarginTop app:layout_goneMarginRight app:layout_goneMarginBottom

居中設置: app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"

偏移:(賦值爲0-1) layout_constraintHorizontal_bias 水平偏移 layout_constraintVertical_bias 垂直偏移

尺寸: 當控件的高度或寬度爲wrap_content時,可使用下列屬性來控制最大、最小的高度或寬度: android:minWidth 最小的寬度 android:minHeight 最小的高度 android:maxWidth 最大的寬度 android:maxHeight 最大的高度

寬高比: app:layout_constraintDimensionRatio="H,2:3"指的是 高:寬=2:3 app:layout_constraintDimensionRatio="W,2:3"指的是 寬:高=2:3

鏈: app:layout_constraintHorizontal_chainStyle app:layout_constraintVertical_chainStyle 裏面可設置三種: CHAIN_SPREAD —— 展開元素 (默認) CHAIN_SPREAD_INSIDE —— 展開元素,但鏈的兩端貼近parent CHAIN_PACKED —— 鏈的元素將被打包在一塊兒

Constraintlayout佈局輔助控件: layout_optimizationLevel 優化 android.support.constraint.Barrier 輔助線 android.support.constraint.Group 組 android.support.constraint.Placeholder 佔位符 android.support.constraint.Guideline 輔助線

TableLayout

表格佈局,適用於多行多列的佈局格式,每一個TableLayout是由多個TableRow組成,一個TableRow就表示TableLayout中的每一行,這一行能夠由多個子元素組成。實際上TableLayout和TableRow都是LineLayout線性佈局的子類。可是TableRow的參數android:orientation屬性值固定爲horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。因此TableRow實際是一個橫向的線性佈局,且因此子元素寬度和高度一致。 ⚠️注意:在TableLayout中,單元格能夠爲空,可是不能跨列,意思是隻能不能有相鄰的單元格爲空。

TableLayout經常使用屬性: android:shrinkColumns:設置可收縮的列,內容過多就收縮顯示到第二行 android:stretchColumns:設置可伸展的列,將空白區域填充滿整個列 android:collapseColumns:設置要隱藏的列

列的索引從0開始,shrinkColumns和stretchColumns能夠同時設置。

子控件經常使用屬性: android:layout_column:第幾列 android:layout_span:佔據列數

AbsoluteLayout

絕對佈局中將全部的子元素經過設置android:layout_x 和 android:layout_y屬性,將子元素的座標位置固定下來,即座標(android:layout_x, android:layout_y) ,layout_x用來表示橫座標,layout_y用來表示縱座標。屏幕左上角爲座標(0,0),橫向往右爲正方,縱向往下爲正方。實際應用中,這種佈局用的比較少,由於Android終端通常機型比較多,各自的屏幕大小。分辨率等可能都不同,若是用絕對佈局,可能致使在有的終端上顯示不全等。

GridLayout

做爲android 4.0 後新增的一個佈局,與前面介紹過的TableLayout(表格佈局)其實有點大同小異; 不過新增了一些東西:跟LinearLayout(線性佈局)同樣,他能夠設置容器中組件的對齊方式;容器中的組件能夠跨多行也能夠跨多列(相比TableLayout直接放組件,佔一行相比較),由於是android 4.0新增的,API Level 14,在這個版本之前的sdk都須要導入項目。這裏不解釋。

經常使用屬性: android:orientation="" vertical(豎直,默認)或者horizontal(水平) android:layout_gravity="" center,left,right,buttom android:rowCount="4" //設置網格佈局有4行 android:columnCount="4" //設置網格佈局有4列

子組件屬性 android:layout_row = "1" //設置組件位於第二行 android:layout_column = "2" //設置該組件位於第三列 android:layout_rowSpan = "2" //縱向橫跨2行 android:layout_columnSpan = "3" //橫向橫跨2列

通用屬性

android:id —— 爲控件指定相應的ID

android:layout_width —— 定義本元素的寬度

android:layout_height —— 定義本元素的高度

android:background —— 指定該控件所使用的背景色,RGB命名法

android:orientation="vertical"     ----horizontal爲控件默認橫向顯示,vertical爲豎着顯示,不設置該屬性默認爲橫向顯示

android:gravity 

android:gravity屬性是對該view 內容的限定.好比一個button 上面的text. 你能夠設置該text 在view的靠左,靠右等位置.以button爲例,android:gravity="right"則button上面的文字靠右

android:layout_gravity

android:layout_gravity是用來設置該view相對與起父view 的位置.好比一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就能夠經過該屬性設置.以button爲例,android:layout_gravity="right"則button靠右

屬性值爲具體的像素值

外間距:

外間距是指控件與控件之間的距離:

android:layout_margin 本元素離父元素上下左右間的距離

android:layout_marginBottom 離父元素底邊緣的距離

android:layout_marginLeft 離父元素左邊緣的距離

android:layout_marginRight 離父元素右邊緣的距離

android:layout_marginTop 離父元素上邊緣的距離

android:layout_marginStart本元素裏開始的位置的距離(至關於離父元素左邊緣的距離)

android:layout_marginEnd本元素裏結束位置的距離(至關於離父元素右邊緣的距離)

內間距:

內間距是指控件以內的內容與該控件的距離間隔:

android:padding指定佈局與子佈局的間距

android:paddingLeft指定佈局左邊與子佈局的間距

android:paddingTop指定佈局上邊與子佈局的間距

android:paddingRight指定佈局右邊與子佈局的間距

android:paddingBottom指定佈局下邊與子佈局的間距

android:paddingStart指定佈局左邊與子佈局的間距與android:paddingLeft相同

android:paddingEnd指定佈局右邊與子佈局的間距與android:paddingRight相同

其它屬性

android:fadingEdgeLength 設置邊框漸變的長度

android:minHeight最小高度

android:minWidth最小寬度

android:translationX 水平方向的移動距離

android:translationY垂直方向的移動距離

android:transformPivotX相對於一點的水平方向偏轉量

android:transformPivotY相對於一點的垂直方向偏轉量

滾動條的設置

android:fadeScrollbars —— 滾動條自動隱藏

android:scrollbarSize —— 設置滾動條大小

android:scrollbars —— 設置滾動條的狀態

android:scrollbarStyle —— 設置滾動條的樣式

android:scrollbarFadeDuration —— 設置滾動條淡入淡出時間

android:scrollbarDefaultDelayBeforeFade —— 設置滾動條N毫秒後開始淡化,以毫秒爲單位。

android:fadingEdge —— 設置拉滾動條時 ,邊框漸變的放向

android:verticalScrollbarPosition —— 設置垂直滾動條的位置

android:scrollbarThumbHorizontal —— 設置水平滾動條的drawable。

android:scrollbarThumbVertical —— 設置垂直滾動條的drawable

android:scrollbarTrackHorizontal —— 設置水平滾動條背景(軌跡)的色drawable

android:scrollbarTrackVertical —— 設置垂直滾動條背景(軌跡)的色drawable

android:scrollbarAlwaysDrawHorizontalTrack —— 設置水平滾動條是否含有軌道

android:scrollbarAlwaysDrawVerticalTrack —— 設置垂直滾動條是否含有軌道

android:requiresFadingEdge —— 定義滾動時邊緣是否褪色

經常使用設置

android:descendantFocusability —— 控制子佈局焦點獲取方式 經常使用於listView的item中包含多個控件 點擊無效

android:visibility —— 定義佈局是否可見

android:clickable —— 定義是否可點擊

android:longClickable —— 定義是否可長點擊

android:saveEnabled —— 設置是否在窗口凍結時(如旋轉屏幕)保存View的數據

android:filterTouchesWhenObscured —— 所在窗口被其它可見窗口遮住時,是否過濾觸摸事件

android:keepScreenOn —— 設置屏幕常亮

android:soundEffectsEnabled —— 點擊或觸摸是否有聲音效果

android:hapticFeedbackEnabled —— 設置觸感反饋

其餘

android:fitsSystemWindows —— 設置佈局調整時是否考慮系統窗口(如狀態欄)

android:drawingCacheQuality —— 設置繪圖時半透明質量

android:OverScrollMode —— 滑動到邊界時樣式

android:alpha —— 設置透明度

android:rotation —— 旋轉度數

android:rotationX —— 水平旋轉度數

android:rotationY —— 垂直旋轉度數

android:scaleX —— 設置X軸縮放

android:scaleY —— 設置Y軸縮放

android:layerType —— 設定支持

android:layoutDirection —— 定義佈局圖紙的方向

android:textDirection —— 定義文字方向

android:textAlignment —— 文字對齊方式

android:importantForAccessibility —— 設置可達性的重要行

android:labelFor —— 添加標籤

android:persistentDrawingCachehua —— 定義繪圖的高速緩存的持久性

android:layoutAnimation —— 定義佈局顯示時候的動畫

android:tag —— 爲佈局添加tag方便查找與相似

android:nextFocusLeft —— 設置左邊指定視圖得到下一個焦點

android:nextFocusRight —— 設置右邊指定視圖得到下一個焦點

android:nextFocusUp —— 設置上邊指定視圖得到下一個焦點

android:nextFocusDown —— 設置下邊指定視圖得到下一個焦點

android:nextFocusForward —— 設置指定視圖得到下一個焦點

android:contentDescription —— 說明

android:OnClick —— 點擊時從上下文中調用指定的方法

android:animateLayoutChanges —— 佈局改變時是否有動畫效果

android:clipToPadding —— 定義佈局間是否有間距

android:animationCache —— 定義子佈局也有動畫效果

android:alwaysDrawnWithCache —— 定義子佈局是否應用繪圖的高速緩存

android:addStatesFromChildren —— 定義佈局是否應用子佈局的背景

android:splitMotionEvents —— 定義佈局是否傳遞touch事件到子佈局

android:focusableInTouchMode —— 定義是否能夠經過touch獲取到焦點

android:isScrollContainer —— 定義佈局是否做爲一個滾動容器 能夠調整整個窗體

android:duplicateParentState —— 是否從父容器中獲取繪圖狀態(光標,按下等)

注意⚠️️️⚠️⚠️佈局的優化很重要,咱們將在後面的文章中介紹

相關文章
相關標籤/搜索