標籤:
Android布局主要有5種,接下來學習總結下。
1) 最常見的線性布局 LinearLayout
線性布局是Android布局中最簡單的布局,也是最常用,最實用的布局。
android:orientation線形布局的對齊 : vertical(垂直) 和 horizontal(水平)
LayoutParams中的特殊參數:
layout_weight 權值layout_gravity 相對於父元素的重力值(預設top|left):(top|bottom|left|right|center_vertical|fill_vertical |center_ horizontal |fill_ horizontal | center| fill)
LinearLayout有兩個非常相似的屬性:
android:gravityandroid:layout_gravity
他們的區別在於:
android:gravity用於設定View中內容相對於View組件的對齊,而android:layout_gravity用於設定View組件相對於Container的對齊。
原理跟android:paddingLeft、android:layout_marginLeft有點類似。如果在按鈕上同時設定這兩個屬性。
android:paddingLeft="30px" 按鈕上設定的內容離按鈕左邊邊界30個像素android:layout_marginLeft="30px" 整個按鈕離左邊設定的內容30個像素
下面回到正題, 我們可以通過設定android:gravity=”center”來讓EditText中的文字在EditText組件中置中顯示;
同時我們設定EditText的android:layout_gravity=”right”來讓EditText組件在LinearLayout中居右顯示。看下效果:
1 <LinearLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" > 6 7 <EditText 8 android:layout_width="wrap_content" 9 android:gravity="center"10 android:layout_height="wrap_content"11 android:text="this is text"12 android:layout_gravity="right"/>13 </LinearLayout>
2) 相對布局 RelativeLayout
RelativeLayout允許子項目指定他們相對於其它元素或父元素的位置(通過ID 指定)。因此,你可以以靠右對齊,或上下,或置於螢幕中央的形式來 排列兩個元素。元素按順序排列,因此如果第一個元素在螢幕的中央,那麼相對於這個元素的其它元素將以螢幕中央的相對位置來排列。如果使用XML 來指定這個 layout ,在你定義它之前,被關聯的元素必須定義。
這個布局是最靈活的布局,因此複雜的布局我們多用這個布局。
LayoutParams中特殊的參數 :<1>. 屬性值為true或false
android:layout_centerHrizontal 水平置中android:layout_centerVertical 垂直置中android:layout_centerInparent 相對於父元素完全置中android:layout_alignParentBottom 貼緊父元素的下邊緣android:layout_alignParentLeft 貼緊父元素的左邊緣android:layout_alignParentRight 貼緊父元素的右邊緣android:layout_alignParentTop 貼緊父元素的上邊緣android:layout_alignWithParentIfMissing 若找不到兄弟元素以父元素做參照物
<2>. 屬性值必須為id的引用名”@id/id-name”
android:layout_below 在某元素的下方android:layout_above 在某元素的上方android:layout_toLeftOf 在某元素的左邊android:layout_toRightOf 在某元素的右邊android:layout_alignBaseLine 該控制項的baseline和給定ID的控制項的Baseline對齊android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
<3>. 屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離android:layout_marginRight 離某元素右邊緣的距離android:layout_marginTop 離某元素上邊緣的距離
下面看一個例子:
1 <RelativeLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" > 6 7 <Button 8 android:layout_weight="1" 9 android:id="@+id/button1"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:text="第一個按鈕"/>13 14 <Button15 16 android:id="@+id/button2"17 android:layout_below="@+id/button1"18 android:layout_toRightOf="@+id/button1"19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:text="第二個按鈕"/>22 23 <Button24 25 android:id="@+id/button3"26 android:layout_below="@+id/button2"27 android:layout_toRightOf="@+id/button2"28 android:layout_width="wrap_content"29 android:layout_height="wrap_content"30 android:text="第三個按鈕"/>31 32 </RelativeLayout>
運行效果:
3) 表格版面配置 TableLayout
TableLayout 將子項目的位置分配到行或列中。一個TableLayout 由許多的TableRow 組成,每個TableRow 都會定義一個 row(事實上,你可以定義其它的子物件)。
TableLayout 容器不會顯示row 、cloumns 或cell 的邊框線。每個row擁有0個或多個的cell;每個cell 擁有一個View對象。
表格由列和行組成許多的儲存格。表格允許儲存格為空白。儲存格不能跨列,這與HTML 中的不一樣。
特殊的參數:
android:stretchColumns 伸展的列的索引android:shrinkColumns 收縮的列的索引android:collapseColumns 倒塌的列的索引(即銷毀)
表示兩行兩列的一個表格。 android:gravity=”center”書面解釋是權重比。其實就是讓它置中顯示。
它還可以動態添加裡面的每行每列。如下代碼所示:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table01);TableRow tableRow = new TableRow(this);TextView temp = new TextView(this);temp.setText("text的值");tableRow.addView(temp);
android:stretchColumns=”1,2,3,4″ 它的意思就是自動展開1,2,3,4列。
4) 絕對布局 AbsoluteLayout
AbsoluteLayout可以讓子項目指定準確的x/y座標值,並顯示在螢幕上。(0, 0)為左上方,當向下或向右移動時,座標值將變大。
AbsoluteLayout沒有頁邊框,允許元素之間互相重疊(儘管不推薦)。我們通常不推薦使用
AbsoluteLayout,除非你有正當理由要使用它,因為它使介面代碼太過剛性,以至於在不同的裝置上可能不能很好地工作。
LayoutParams中特殊的參數 :
layout_x x方向的座標layout_y y方向的座標
5) 幀布局 FrameLayout
FrameLayout是最簡單的一個布局對象。它被定製為你螢幕上的一個空白備用地區,之後你可以在其中填充一個單一對象。比如,一張你要發布的圖片。所有的子項目將會固定在螢幕的左上方;你不能為FrameLayout中的一個子項目指定一個位置。後一個子項目將會直接在前 一個子項目之上進行覆蓋填充,把它們部份或全部擋住(除非後一個子項目是透明的)。
裡面可以放多個控制項,不過控制項的位置都是相對位置
LayoutParams中特殊的參數 :
layout_gravity 相對於父元素的重力值(用法同LinearLayout)
注意事項:
各布局不要亂用各自的屬性。比如把屬於AbsoluteLayout布局的android:layout_x和android:layout_y用到LinearLayout布局或RelativeLayout布局,或者把RelativeLayout布局的 below,rightof等屬性應用到其他布局中。這樣做雖然不會報錯,但是根本達不到我們需要的效果。
關於android:layout_width=”fill_parent”和android:layout_height=”wrap_content” ,這是對每個布局寬和高的設定。wrap_content可表示隨著其中控制項的不同而改變這個布局的寬度或高度,類似於自動化佈建寬和高,fill_parent使布局填充整個螢幕,另外還有一種match_parent,它本質上和 fill_parent 一樣,並從API Level8開始替代fill_parent。
參考:http://blog.csdn.net/tcpipstack/article/details/18711597
14.Android之Layout布局學習