標籤:跳過 width abs 控制項 距離 推出 reg 如何使用 精通
在Android中提供了幾個常用布局:
LinearLayout
線性布局
RelativeLayout
相對布局
FrameLayout
幀布局
AbsoluteLayout
絕對布局
TableLayout
表格版面配置
GridLayout
網格布局
TableLayout表格版面配置
TableLayout的介紹
TableLayout
是將子類向分別排列成行和列的布局視圖容器,TableLayout
是由許多TableRow
對象組成的,表格版面配置以行列的形式管理子控制項,每一個單元是一個TableRow
或者View
對象。
在TableLayout
中可以通過setConlumnShrinkable()
或setConlumnStretchable()
方法來指定某些列為可以縮小或可伸縮,列是從0開始計數的,第一列為0。
屬性
常用的幾種屬性:
stretchColumns
為設定運行被展開的列的序號,如android:stretchColumns="2,3"
表示在第三列的和第四列的一起填補空白,如果要所有列一起填補空白,則用“*”
符號,列號都是從0開始算的。
shrinkColumns
為設定被收縮的列的序號,收縮是用於在一行中列太多或者某列的內容文本過長,會導致某列的內容會被擠出螢幕,這個屬性是可以協助某列的內容進行收縮,用於防止被擠出的。
android:collapseColumns
為設定需要被隱藏的列的序號,使用該屬性可以隱藏某列。
android:layout_column
為為該子類控制項顯示在第幾列。android:layout_column="2"
表示跳過第二個,直接顯示在第三個儲存格內。
android:layout_span
為為該子類控制項佔據第幾列。android:layout_span="3"
表示合并3個儲存格,就是這個組件將佔據3個儲存格。
collapseColumns隱藏列
效果
android:collapseColumns = "0,2"
,用於隱藏第一列和第三列,代碼如下:
<TableLayout android:id="@+id/TableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="0,2" > <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="two" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="three" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="four" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="five" /> </TableRow></TableLayout>
stretchColumns展開列
android:stretchColumns = “1”
,設定為第二列為可展開列的列,讓該列填滿這一行所有的剩餘空間,也就是在整個父寬度的情況在,放幾個按鈕,剩下的空間寬度將用第二列填滿,代碼如下:
<TableLayout android:id="@+id/TableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="two" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="three" /> </TableRow></TableLayout>
shrinkColumns收縮列
android:shrinkColumns="1"
表示將第二列的內容進行收縮,如果螢幕的額寬度包容不下的話,就會拿第二列進行收縮,就是壓扁,拉長。如同上代碼進行修改即可,多加些內容,讓其常值內容超出螢幕吧!
GridLayout網格布局
GridLayout
網格布局是在Android 4.0
以後引入的一種新的配置模式,和表格版面配置是有點類似的,但比表格版面配置的好,功能也是很強大的,它可以設定布局有多少行和有多少列,也可以設定布局中的組件的相片順序,也可以設定組件的位置,橫跨多少行,多少列。
屬性的那點東西
android:orientation
用於設定vertical
豎直或horizontal
水平。
android:layout_gravity
為設定對齊,可以設定center,right,left
等。
android:rowCount
為可以設定行數,要多少行設定多少行,如android:rowCount="2"
為設定網格布局有2行。
android:columnCount
為可以設定列數,要多少列設定多少列,如android:columnCount="2"
為設定網格布局有2列。
android:layout_row
為設定組件位於第幾行,從0開始計數的,如android:layout_row="1"
為設定組件在第2行。
android:layout_column
為設定組件位於第幾列,從0開始計數的,如android:layout_column="1"
為設定組件在第2列。
android:layout_rowSpan
為設定組件橫跨幾行,如android:layout_rowSpan="2"
為縱向橫跨2行。
android:layout_columnSpan
為設定組件橫跨幾列,如android:layout_columnSpan="2"
為橫向橫跨2列。
注意事項:低版本sdk是如何使用GridLayout
的呢?要匯入v7包的gridlayout
包即可,因為GirdLayout
是4.0後才推出的,標籤代碼。
<android.support.v7.widget.GridLayout>
FrameLayout幀布局
FrameLayout
幀布局是什麼樣的呢?所有子控制項都放在左上方且後面元素都是直接覆蓋在前面元素之上一種配置模式。
常用屬性:
android:foreground
為設定改幀版面配置容器的前景映像,什麼是前景映像,前景映像是永遠處於幀版面配置容器的最上面的映像,就是不會被覆蓋的圖片。
android:foregroundGravity
為設定前景映像顯示的位置。
列子代碼
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="200dp" android:layout_height="200dp" android:background="#FF6143" /> <TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#7BFE00" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFF00" /></FrameLayout>
用幀布局可以做出霓虹燈的效果,即為每個TextVeiw
設定layout_gravity="center"
即可,即可以看到不一樣的效果。
AbsoluteLayout絕對布局
AbsoluteLayout
絕對布局是通過x,y
位置來為子控制項設定位置的,即android:layout_x和android:layout_y
屬性。
由於絕對布局不常見,不常用,因為在不同大小的適配螢幕上的位置直觀上會變化,適應能力差,所以不建議使用。
RelativeLayout相對布局
RelativeLayout
是一個相對布局的視圖組,用來顯示相對位置的子視圖類,在預設情況下,所有子視圖對會分布在左上方。
這裡簡單溫習一下相關屬性即可:
android:layout_below
為位於某控制項下方,以id為標記
android:layout_above
為位於某控制項上方,以id為標記
android:layout_toLeftOf
為位於某控制項左方,以id為標記
android:layout_toRightOf
為位於某控制項右方,以id為標記
android:layout_alignBottom
為與某控制項底部對齊,以id為標記
android:layout_alignTop
為與某控制項頂部對齊,以id為標記
android:layout_alignLeft
為與某控制項左邊緣對齊,以id為標記
android:layout_alignRight
為與某控制項右邊緣對齊,以id為標記
android:layout_alignBaseline
為與某控制項的常值內容在一條直線上
android:layout_alignParentBottom
為在父容器最下,為true或false
android:layout_alignParentTop
為在父容器最上,為true或false
android:layout_alignParentLeft
為在父容器最左,為true或false
android:layout_alignParentRight
為在父容器最右,為true或false
android:layout_marginTop
為和父容器上端的距離,單位為dp
android:layout_marginBottom
為和父容器下端的距離,單位為dp
android:layout_marginLeft
為和父容器左端的距離,單位為dp
android:layout_marginRight
為和父容器右端的距離,單位為dp
android:layout_margin
為和父容器四周的距離,單位為dp
android:layout_centerVertical
為在父類的垂直置中,為true或false
android:layout_centerHorizontal
為在父類的水平置中,為true或false
android:layout_centerInParent
為在父類的水平垂直置中
結論
線性布局:
指子控制項以水平或垂直方式排列。
相對布局:
指子控制項以控制項之間的相對位置或子控制項相對於父容器的位置排列。
幀布局:
指所有子控制項均放在左上方且後面元素直接覆蓋在前面元素之上。
絕對布局:
指子控制項通過絕對位置x,y位置來決定其位置擺放。
表格版面配置:
指以行列的形式放置子控制項,每一行是一個TableRow對象或者View對象。
結語
- 本文主要講解?Android精通:TableLayout布局,GridLayout網格布局,FrameLayout幀布局,AbsoluteLayout絕對布局,RelativeLayout相對布局
- 下面我將繼續對
Java
、?Android
中的其他知識 深入講解 ,有興趣可以繼續關注
- 小禮物走一走 or 點贊
Android精通:TableLayout布局,GridLayout網格布局,FrameLayout幀布局,AbsoluteLayout絕對布局,RelativeLayout相對布局