先說一下一些可能通用的參數
android:layout_width
& android:layout_height
:佈局的寬和高,一般不用改,參數選 match_parent 就行
android:background
:佈局的背景
android:paddingBottom
&android:paddingRight
&android:paddingLeft
&android:paddingTop
:佈局的上下左右邊距
android:gravity
:設置佈局的重心,也就是組件會往哪裏靠,比如如果參數是 center 的話,所有的組件都會在屏幕中間排列,其它同理。可以同時使用多個參數,用 | 分割,如 right|bottom
便是設置爲右下角
android:ignoreGrivaty
:設置哪些組件不需要遵循上面設置的 gravity ,參數是佈局中組件的 id
相對佈局管理器 RelativeLayout
顧名思義,裏面所有的元素的位置都是相對的。
教程裏提到的參數有十五個,但要注意都是佈局中子組件的參數而不是佈局本身的參數。
android:layout_above
&android:layout_below
&android:layout_toLeftOf
&android:layout_toRightOf
:參數爲其它組件的 id ,設置該組件位於對應 id 的組件外部的上下左右
android:layout_alignParentBottom
&android:layout_alignParentTop
&android:layout_alignParentRight
&android:layout_alignParentLeft
:參數爲 ture ,設置組件位於佈局的上下左右位置
android:layout_alignBottom
&android:layout_alignTop
&android:layout_alignRight
&android:layout_alignLeft
:參數爲其它組件的 id ,設置該組件位於對應 id 的組件內部的上下左右位置
android:layout_centerHorizontal
&android:layout_centerParent
&android:layout_centerVertical
:參數應該也爲 true ,設置該組件水平居中、佈局正中間、豎直居中
線性佈局管理器 LinearLayout
所有組件都是線性排布。
android:orientation
:參數爲 horizontal 則該佈局管理器爲水平佈局管理器,參數爲 vertical 則爲垂直線性佈局管理器
子組件參數:android:layout_weight
:設置子組件的權重(分配剩餘空間用),用了這個參數的話大概就會充分佔據剩餘空間,填滿一整行或者一整列
幀佈局管理器 FrameLayout
顧名思義,所有組件是一幀幀層疊排布的,有點像 PS 裏圖層的概念。通常用於顯示層疊的內容,還可以實現拖動效果。
android:foreground
:爲幀佈局管理器設置前景圖像(前景圖像指始終位於最上層的圖像)
android:foregroundGravity
:設置前景圖像的位置
表格佈局管理器 TableLayout
以行和列的方式管理子組件的佈局管理器。通過<TableRow>
標籤設置一行,標籤對中每添加一個組件則爲行中的一列。如果不用<TableRow>
標籤則一個組件佔一行。
android:collapseColumns
:設置哪些列被隱藏,參數爲列的序號,從零開始計,多個序號間用逗號分隔
android:stretchColumns
:設置哪些列可以被拉伸,參數同上。設置了這個參數的話一行將會被填滿,效果和上面線性佈局管理器的android:layout_weight
挺像的,但是要注意,這個是佈局管理器的參數,不是子組件的參數,並且是同時控制所有行的
android:shrinkColumns
:設置哪些列可以被壓縮,參數同上。當一行顯示不下所有組件時,該參數設置的列將會被壓縮
網格佈局管理器 GridLayout
與表格佈局管理器的區別在於,表格只能跨列,網格則是行和列都可以跨。同時表格一行顯示不下會跑到屏幕外面去,網格則會(在達到設置的最大列數後)自動換行。
android:columnCount
:設置最大列數
android:rowCount
:設置最大行數
android:oritentation
:設置水平還是豎直,和線性佈局管理器一樣
子組件參數:
android:layout_grivaty
:設置子組件位於網格空間的何處
android:layout_cloumn
&android:layout_row
:設置子組件位於第幾列/第幾行,參數爲行或列的序號(整數)
android:layout_cloumnSpan
&android:layout_rowSpan
:設置子組件跨幾列/幾行,參數爲一個整數,搭配android:layout_grivaty="fill"
可以使子組件填充滿這幾行/幾列空間
android:layout_columWeight
&android:layout_rowWeight
:設置子組件列與行的權重
最後
如果你看到了這裏,覺得文章寫得不錯就給個讚唄!歡迎大家評論討論!如果你覺得那裏值得改進的,請給我留言。一定會認真查詢,修正不足,定期免費分享技術乾貨。喜歡的小夥伴可以關注一下哦。謝謝!