跟我學android-android常用布局介紹,android常用布局
在上一章我們曾經談到,Android平台的介面 是使用XML的方式設計的,然後在上一章我們只做了一個簡單的介面,在這章,我們將介紹如何使用常用的控制項設計實用的介面。
Android中的視圖都是繼承View的。Android的視圖分2種,一種是普通的視圖,一種是ViewGroup。他們的區別在於,ViewGroup裡可以放很多普通視圖,我們常見的布局就是一種ViewGroup。
Android中布局是用來排版的,以下是Android中常用的布局列表
名稱 |
說明 |
LinearLayout |
線性布局 |
RelativeLayout |
相對布局 |
FrameLayout |
幀布局 |
TableLayout |
表格版面配置 |
AbsoluteLayout |
絕對布局(廢棄) |
GridLayout |
網格布局(4.0推出的) |
接下來我們對每種布局進行介紹
LinearLayout
LinearLayout 我們翻譯為線性布局,他的子項目是水平和垂直方向進行排列,可以通過設定 Orientation 改變相片順序,我們看下效果
首先 建立一個布局檔案 linearlayout_demo.xml ,右鍵layout檔案夾 選擇 new-Android xml File
在彈出的視窗中輸入 布局檔案的名稱
Finish後,我們的布局檔案就建立好了。
接下來 我們往 linearlayout.xml布局裡拖入多個文本控制項
目前我拖入了5個文本控制項,他是按照從上往下的順序排列的,即垂直排列
接下來 我們修改 orientation的屬性為horizontal,5個文本控制項的相片順序方式了改變,變成了水平方向 的了.接著我們切換到代碼地區
android:orientation 是設定LinearLayout排列方向的,也是LinearLayout特有的屬性。
RelativeLayout
RelativeLayout 即相對布局
同樣我們也建立一個 relativelayout_demo.xml 的布局檔案,選擇根節點為RelativeLayout
隨意拖幾個控制項進入布局
可以看到 RelativeLayout 和LinearLayout 的區別,RelativeLayout 可以任意擺放控制項
RelativeLayout裡常用的位置屬性如下:
android:layout_toLeftOf —— 該組件位於引用組件的左方
android:layout_toRightOf —— 該組件位於引用組件的右方
android:layout_above —— 該組件位於引用組件的上方
android:layout_below —— 該組件位於引用組件的下方
android:layout_alignParentLeft —— 該組件是否對齊父組件的左端
android:layout_alignParentRight —— 該組件是否齊其父組件的右端
android:layout_alignParentTop —— 該組件是否對齊父組件的頂部
android:layout_alignParentBottom —— 該組件是否對齊父組件的底部
android:layout_centerInParent —— 該組件是否相對於父組件置中
android:layout_centerHorizontal —— 該組件是否橫向置中
android:layout_centerVertical —— 該組件是否垂直置中
TableLayout
TableLayout顧名思義,此布局為表格版面配置,適用於N行N列的布局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。
TableRow是LinearLayout的子類,它的android:orientation屬性值恒為horizontal,並且它的android:layout_width和android:layout_height屬性值恒為MATCH_PARENT和WRAP_CONTENT。所以它的子項目都是橫向排列,並且寬高一致的。這樣的設計使得每個TableRow裡的子項目都相當於表格中的儲存格一樣。在TableRow中,儲存格可以為空白,但是不能跨列。
GridLayout(4.0以上的版本支援,所以如果想直接使用該布局的話,需要設定最低版本號碼為14)
android4.0以上版本出現的GridLayout布局解決了以上問題。GridLayout布局使用虛細線將布局劃分為行、列和儲存格,也支援一個控制項在行、列上都有交錯排列。而GridLayout使用的其實是跟LinearLayout類似的API,只不過是修改了一下相關的標籤而已,所以對於開發人員來說,掌握GridLayout還是很容易的事情。GridLayout的布局策略簡單分為以下三個部分:
首先它與LinearLayout布局一樣,也分為水平和垂直兩種方式,預設是水平布局,一個控制項挨著一個控制項從左至右依次排列,但是通過指定android:columnCount設定列數的屬性後,控制項會自動換行進行排列。另一方面,對於GridLayout布局中的子控制項,預設按照wrap_content的方式設定其顯示,這隻需要在GridLayout布局中顯式聲明即可。
其次,若要指定某控制項顯示在固定的行或列,只需設定該子控制項的android:layout_row和android:layout_column屬性即可,但是需要注意:android:layout_row=”0”表示從第一行開始,android:layout_column=”0”表示從第一列開始,這與程式設計語言中一維數組的賦值情況類似。
最後,如果需要設定某控制項跨越多行或多列,只需將該子控制項的android:layout_rowSpan或者layout_columnSpan屬性設定為數值,再設定其layout_gravity屬性為fill即可,前一個設定表明該控制項跨越的行數或列數,後一個設定表明該控制項填滿所跨越的整行或整列。
GridLayout 常用的屬性有
android:columnCount 設定列數
android:rowCount 設定行數
android:layout_rowSpan=指定控制項占幾行
android:layout_column=指定控制項在第幾列
android:layout_row=指定控制項在第幾行
比如 我們想做一個小鍵盤
tabLeLayout 做不到的,因為他不可以跨列,LinearLayout可以做到 但是需要嵌套很多層,如果使用GridLayout 就可以很簡單的做出來
代碼如下
1 <?xml version="1.0" encoding="utf-8"?> 2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:columnCount="4" > 6 7 <Button 8 android:id="@+btn_1_1" 9 android:layout_column="1"10 android:text="print" />11 12 <Button13 android:id="@+btn_1_2"14 android:text="Scro" />15 16 <Button17 android:id="@+btn_1_3"18 android:text="Pau" />19 20 <Button21 android:id="@+btn_2_1"22 android:text="Num" />23 24 <Button25 android:id="@+btn_2_2"26 android:text="/" />27 28 <Button29 android:id="@+btn_2_3"30 android:text="*" />31 32 <Button33 android:id="@+btn_2_4"34 android:text="-" />35 36 <Button37 android:id="@+btn_3_1"38 android:text="7" />39 40 <Button41 android:id="@+btn_3_2"42 android:text="8" />43 44 <Button45 android:id="@+btn_3_3"46 android:text="9" />47 48 <Button49 android:id="@+btn_3_4"50 android:layout_gravity="fill_vertical"51 android:layout_rowSpan="2"52 android:text="+" />53 54 <Button55 android:id="@+btn_4_1"56 android:text="4" />57 58 <Button59 android:id="@+btn_4_2"60 android:text="5" />61 62 <Button63 android:id="@+btn_4_3"64 android:text="6" />65 66 <Button67 android:id="@+btn_5_1"68 android:text="1" />69 70 <Button71 android:id="@+btn_5_2"72 android:text="2" />73 74 <Button75 android:id="@+btn_5_3"76 android:text="3" />77 78 <Button79 android:id="@+btn_5_4"80 android:layout_gravity="fill_vertical"81 android:layout_rowSpan="2"82 android:text="Ent" />83 84 <Button85 android:id="@+btn_6_1"86 android:layout_columnSpan="2"87 android:layout_gravity="fill_horizontal"88 android:text="0" />89 90 <Button91 android:id="@+btn_6_2"92 android:text="." />93 94 </GridLayout>
常用布局 簡單的給大家介紹到這個地方, 接下來我們介紹Android常用的控制項
介紹下Android中常用的五種布局,分別的布局方式急
Linearlayout 線性布局
Relativelayout 相對布局
FrameLayout 架構布局
TableLayout 表格版面配置
AbsoluteLayout 絕對布局(不建議使用,最常用的還是以上四種)
android有什布局管理器,什最常用?
LinearLayout ,TableLayout,RelativeLayout,FrameLayout還有一個不推薦使用的AbsoluteLayout
初學比較常用的是LinearLayout ,以後寫介面用的多的是RelativeLayout~~