標籤:android style blog http io color ar 使用 sp
Android的布局類型:
主要有:LinearLayout(線性布局)、RelativeLayout(相對布局)、TableLayout(表格版面配置)、AbsoluteLayout(絕對布局)、FrameLayout(單幀布局)。
1、LinearLayout(線性布局):是5種布局最常用的一種,這種布局在顯示組件的時候會預設保持組件之間的間隔以及組件之間的互相對齊。
(1)顯示組件兩種方式:垂直(vertical)和水平(horizontal),是通過orientation來設定的。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" //垂直排列(水平為horizontal)
> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button2" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button3" /></LinearLayout>
(2)可嵌套使用線性布局。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button3" /> </LinearLayout></LinearLayout>
總結:線性布局裡頭一些常用的屬性如下
1)android:id - 為控制項指定相應的ID(例如:android:id="@+id/txt" )
2)android:text - 指定控制項當中顯示的文字,注意,盡量使用string.xml(例如android:text = "@string/hello_world")
3)android:gravity - 指定控制項的基本位置,比如說置中,居右等位置 (例如android:gravity="center_vertical" )
android:layout_gravity-是相對與它的父元素說的,說明元素顯示在父元素的什麼位置。
每個組件預設其值為左上方對齊,其屬性可以調整組件對齊比如向左、向右或者置中對齊等。
可選的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且這些屬性是可以多選的,用“|”分開。預設這個的值是:Gravity.LEFT。
當 android:orientation="vertical" 時, 只有水平方向的設定才起作用,垂直方向的設定不起作用。即:left,right,center_horizontal 是生效的。
當 android:orientation="horizontal" 時, 只有垂直方向的設定才起作用,水平方向的設定不起作用。即:top,bottom,center_vertical 是生效的。
4)android:textSize - 指定控制項當中字型的大小 (例如android:textSize="15pt" )
5)android:background - 指定控制項所用的背景色,RGB命名法(android:background="#aa0000" )
6)android:layout_width - 指定控制項的寬度(例如android:layout_width="fill_parent" )
一般是"fill_parent","wrap_content","match_parent",也可以設定數值。
7) android:layout_height - 指定控制項的高度(例如android:layout_height="wrap_content" )
一般是"fill_parent","wrap_content","match_parent",也可以設定數值。
8)android:layout_weight - 指定控制項的佔用比例(例如android:layout_weight="2" )
預設為零,其屬性工作表示當前還有多大視圖就佔據多大視圖;如果其值高於零,則表示將父視圖中的可用的空間進行分割,分割的大小視當前螢幕整體布局的Layout_weight值與每個組件Layout_weight值佔用比例來定。
9)android:padding - 指定控制項的內邊距,也就是說控制項當中的內容 (例如:android:paddingLeft="10dip" )
共有paddingTop、paddingBottom、paddingLeft、paddingRight、padding五種。
10)android:sigleLine - 如果設定為真的話,則將控制項的內容顯示在一行當中(例如android:singleLine="true" )
11)android:layout_margin,外邊距,其上下左右屬性為:layout_marginTop,layout_marginBottom,layout_marginLeft,layout_marginRight
注意:線性布局中
[1]帶"layout"的屬性是指整個控制項與父控制項之間的關係,如 layout_gravity 在父控制項中的對齊, layout_margin 是層級相同的控制項之間的間隙等等;
[2]不帶"layout" 的屬性是指控制項中文本的格式,如gravity是指文本的對齊等等,而其中文本的格式又受制約於它的控制項在父控制項中的屬性.
2、RelativeLayout(相對布局)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" android:id="@+id/btn1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button2" android:id="@+id/btn2" android:layout_below="@id/btn1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" android:id="@+id/btn3" android:layout_below="@id/btn2" android:layout_alignRight="@id/btn2" /> </RelativeLayout>
總結:相對布局常用屬性
(1)組件之間的位置關係:
[1]android:layout_above :將該控制項的底部至於給定ID的控制項之上;
[2]android:layout_below :將該控制項的頂部至於給定ID的控制項之下;
[3]android:layout_toLeftOf :將該控制項的右邊緣和給定ID的控制項的左邊緣對齊;
[4]android:layout_toRightOf :將該控制項的左邊緣和給定ID的控制項的右邊緣對齊;
(2)組件對齊:
[1]android:layout_alignBaseline:該控制項的baseline和給定ID的控制項的baseline對齊;
[2]android:layout_alignBottom:將該控制項的底部邊緣與給定ID控制項的底部邊緣對齊;
[3]android:layout_alignLeft:將該控制項的左邊緣與給定ID控制項的左邊緣對齊;
[4]android:layout_alignRight:將該控制項的右邊緣與給定ID控制項的右邊緣對齊;
[5]android:layout_alignTop:將給定控制項的頂部邊緣與給定ID控制項的頂部對齊;
(3)當前組件與父組件的對齊:
[1]android:alignParentBottom:true,則將該控制項的底部和父控制項的底部對齊;
[2]android:layout_alignParentLeft:true,則將該控制項的左邊與父控制項的左邊對齊;
[3]android:layout_alignParentRight:true,則將該控制項的右邊與父控制項的右邊對齊;
[4]android:layout_alignParentTop:true,則將空間的頂部與父控制項的頂部對齊;
(4)組件放置的位置:
[1]android:layout_centerHorizontal:true,該控制項將被至於水平方向的中央;
[2]android:layout_centerInParent:true,該控制項將被至於父控制項水平方向和垂直方向的中央;
[3]android:layout_centerVertical:true,該控制項將被至於垂直方向的中央;
Android 之布局(一)