一、LinearLayout:線性布局
用來控制其子View以水平或垂直方式展開顯示
重要屬性
orientation(方向)
layout_weight(權重)
layout_weight(權重)的值
=0(默認值):指定多大空間就占據多大的空間
>0:將父視圖中的可用空間進行分割, 值越大權重就越大, 占據的比例就會越大
Layout_weight的使用場景
將布局的寬度或高度平均分成幾個等份
垂直方向上占用中間所有空間 或 水平方向上占用中間所有空間
android:gravity 與 android:layout_gravity的區別
android:gravity是指定本元素的子元素相對它的對齊方式。
android:layout_gravity是指定本元素相對它的父元素的對齊方式
案例參考:http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html
代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".LinearLayoutActivity" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:layout_weight="1" 12 android:orientation="horizontal" > 13 14 <Button 15 android:layout_width="wrap_content" 16 android:layout_height="match_parent" 17 android:layout_weight="1" 18 android:background="#aa0000" 19 android:gravity="center_horizontal|center_vertical" 20 android:text="第一列" 21 android:textSize="15sp" > 22 </Button> 23 24 <Button 25 android:layout_width="wrap_content" 26 android:layout_height="match_parent" 27 android:layout_weight="1" 28 android:background="#00aa00" 29 android:gravity="center_horizontal" 30 android:text="第二列" 31 android:textSize="15sp" > 32 </Button> 33 34 <Button 35 android:layout_width="wrap_content" 36 android:layout_height="match_parent" 37 android:layout_weight="1" 38 android:background="#0000aa" 39 android:gravity="center|bottom" 40 android:text="第三列" 41 android:textSize="15sp" > 42 </Button> 43 44 <Button 45 android:layout_width="wrap_content" 46 android:layout_height="match_parent" 47 android:layout_weight="1" 48 android:background="#aaaa00" 49 android:gravity="bottom" 50 android:text="第四列" 51 android:textSize="15sp" > 52 </Button> 53 </LinearLayout> 54 55 <LinearLayout 56 android:layout_width="match_parent" 57 android:layout_height="match_parent" 58 android:layout_weight="1" 59 android:orientation="vertical" > 60 61 <Button 62 android:layout_width="match_parent" 63 android:layout_height="match_parent" 64 android:layout_weight="1" 65 android:gravity="bottom" 66 android:text="第1行" 67 android:textSize="15sp" > 68 </Button> 69 70 <Button 71 android:layout_width="match_parent" 72 android:layout_height="match_parent" 73 android:layout_weight="1" 74 android:gravity="bottom" 75 android:text="第2行" 76 android:textSize="15sp" > 77 </Button> 78 79 <Button 80 android:layout_width="match_parent" 81 android:layout_height="match_parent" 82 android:layout_weight="1" 83 android:gravity="bottom" 84 android:text="第3行" 85 android:textSize="15sp" > 86 </Button> 87 88 <Button 89 android:layout_width="match_parent" 90 android:layout_height="match_parent" 91 android:layout_weight="1" 92 android:gravity="bottom" 93 android:text="第4行" 94 android:textSize="15sp" > 95 </Button> 96 </LinearLayout> 97 98 </LinearLayout>
二、RelativeLayout:相對布局
相對布局: 用來控制其子View以相對定位的方式進行布局顯示
相對布局是最靈活, 最強大,也是學習難度最大的布局
相對布局相關屬性比較多:
兄弟視圖之間: 同方向對齊, 反方向對齊
與父視圖之間: 同方向對齊, 居中
相對兄弟視圖定位
同方向對齊屬性
android:layout_alignBaseline 該控件的Baseline與給定ID控件的Baseline對齊
android:layout_alignLeft 該控件的左邊與給定ID控件的左邊對齊
android:layout_alignTop 該控件的頂部與給定ID控件的頂部對齊
android:layout_alignRight 該控件的右邊與給定ID控件的右邊對齊
android:layout_alignBottom 該控件的底部與給定ID控件的底部對齊
反方向對其屬性
android:layout_toLeftOf 將該控件置於給定ID控件的左邊
android:layout_toRightOf 將該控件置於給定ID控件的右邊
android:layout_above 將該控件置於給定ID控件的上面
android:layout_below 將該控件置於給定ID控件的下面
相對父視圖定位
與父視圖同方向對齊
android:layout_alignParentLeft 該控件的左邊與父控件的左邊對齊
android:layout_alignParentTop 該控件的頂部與父控件的頂部對齊
android:layout_alignParentRight 該控件的右邊與父控件的右邊對齊
android:layout_alignParentBottom 該控件的底部與父控件的底部對齊
相對父視圖居中屬性
android:layout_centerInParent 垂直水平居中
android:layout_centerVertical 垂直居中
android:layout_centerHorizontal 水平居中
案例:
代碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/ed_relative_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:hint="Message"> <requestFocus /> </EditText> <Button android:id="@+id/btn_relative_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ed_relative_msg" android:layout_alignRight="@id/ed_relative_msg" android:text="Ok"/> <Button android:id="@+id/btn_relative_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/btn_relative_ok" android:layout_toLeftOf="@id/btn_relative_ok" android:layout_marginRight="10dp" android:text="Cancle"/> </RelativeLayout>
三、FrameLayout:幀布局
幀布局中的每一個子View都代表一個畫面,默認以屏幕左上角作為( 0,0 )坐標
按定義的先后順序依次逐屏顯示 , 后面出現的會覆蓋前面的畫面
通過子View的android:layout_gravity?屬性來指定子視圖的位置
屬性名稱 | 對應方法 | 描述 |
android:foreground | setForeground(Drawable) | 設置繪制在所有子控件之上的內容 |
android:foregroundGravity | setForegroundGravity(int) | 設置繪制在所有子控件之上內容的gravity屬性 |
在FreamLayout中,子控件是通過棧來繪制的,所以后添加的子控件會被控制在上層。
案例:參考:http://www.cnblogs.com/moonsilvering/archive/2011/12/30/2308091.html
代碼:
1: <?xml version="1.0" encoding="utf-8"?> 2: <resources> 3: <string name="app_name">FrameExample</string> 4: <string name="big">大的</string> 5: <string name="middle">中的</string> 6: <string name="small">小的</string> 7: </resources> 3)在項目res/values目錄下新建一個colors.xml,在其中輸入如下代碼。 1: <?xml version="1.0" encoding="UTF-8"?> 2: <resources> 3: <color name="red">#FF0000</color> 4: <color name="green">#00FF00</color> 5: <color name="blue">#0000FF</color> 6: <color name="white">#FFFFFF</color> 7: </resources> 4)打開項目res/layout目錄下的main.xml文件,將其中已有的代碼替換為如下代碼。 1: <?xml version="1.0" encoding="utf-8"?> 2: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3: android:id="@+id/FrameLayout01" 4: android:layout_width="fill_parent" 5: android:layout_height="fill_parent" 6: android:background="@color/white" > 7: 8: <TextView 9: android:id="@+id/TextView01" 10: android:layout_width="wrap_content" 11: android:layout_height="wrap_content" 12: android:text="@string/big" 13: android:textColor="@color/green" 14: android:textSize="60dp" > 15: </TextView> 16: 17: <TextView 18: android:id="@+id/TextView02" 19: android:layout_width="wrap_content" 20: android:layout_height="wrap_content" 21: android:text="@string/middle" 22: android:textColor="@color/red" 23: android:textSize="40dp" > 24: </TextView> 25: 26: <TextView 27: android:id="@+id/TextView03" 28: android:layout_width="wrap_content" 29: android:layout_height="wrap_content" 30: android:text="@string/small" 31: android:textColor="@color/blue" 32: android:textSize="20dp" > 33: </TextView> 34: 35: </FrameLayout>
一下為常用的一些基本屬性
常用基本屬性四、屬性的划分
針對任何View的屬性
常用的最基本屬性
內邊距屬性 padding
外邊距屬性 margin
只針對RelativeLayout的屬性
反方向對齊屬性 to/above/below
同方向對齊屬性 align
相對父視圖的屬性 alignparent/center
只針對LinearLayout的屬性
權重屬性 weight
方向屬性 oritation
id 為控件指定相應的ID
@+id/idname 添加一個ID
layout_width 指定當前視圖的寬度
layout_height 指定當前視圖的寬度
text 指定控件當中顯示的文字
textSize 指定控件當中字體的大小
background 指定該控件所使用的背景(圖片|顏色)
layout_gravity 控件本身相對於父視圖的位置
grivity 指定控件中的內容的基本位置
內邊距與外邊距(公共屬性)
內邊距:
android:padding
android:paddingLeft
android:paddingTop
android:paddingRight
android:paddingBottom
外邊距
android:layout_margin
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom