【轉】Android入門學習_代碼經常使用佈局 java
一、線性佈局 LinearLayout:
線性佈局是全部佈局中最經常使用的類之一,也是RadioGroup, TabWidget, TableLayout, TableRow,
ZoomControls類的父類。LinearLayout可讓它的子元素垂直或水平的方式排成一行(不設置方
向的時候默認按照垂直方向排列)。
舉個例子:
java代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android "
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!--android:id —— 爲控件指定相應的ID
android:text —— 指定控件當中顯示的文字,須要注意的是,這裏儘可能使用strings.xml文件當中的
字符串
android:grivity —— 指定控件的基本位置,好比說居中,居右等位置
android:textSize —— 指定控件當中字體的大小
android:background —— 指定該控件所使用的背景色,RGB命名法
android:width —— 指定控件的寬度
android:height —— 指定控件的高度
android:padding* —— 指定控件的內邊距,也就是說控件當中的內容
android:layout_weight —— 控件之間的權重比
android:sigleLine —— 若是設置爲真的話,則將控件的內容在同一行當中進行顯示
-->
<TextView
android:id="@+id/firstText"
android:text="第一行一行一行一行一行一行一行一行一行一行"
android:gravity="center_vertical"
android:textSize="35pt"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="20dip"
android:paddingRight="30dip"
android:paddingBottom="40dip"
android:layout_weight="1"
android:singleLine="true"/>
<TextView
android:id="@+id/secondText"
android:text="第二行"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#0000aa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
二、相對佈局 RelativeLayout
相對佈局 RelativeLayout 容許子元素指定它們相對於其父元素或兄弟元素的位置,這是實際佈局
中最經常使用的佈局方式之一。它靈活性大不少,固然屬性也多,操做難度也大,屬性之間產生衝突
的的可能性也大,使用相對佈局時要多作些測試。
舉個例子:
java代碼:
<?xml version="1.0" encoding="utf-8"?>
<!--android:layout_above 將該控件的底部至於給定ID的控件之上
android:layout_below 將該控件的頂部至於給定ID的控件之下
android:layout_toLeftOf 將該控件的右邊緣和給定ID的控件的左邊緣對齊
android:layout_toRightOf 將該控件的左邊緣和給定ID的控件的右邊緣對齊
android:layout_alignBaseline 該控件的baseline和給定ID的控件的baseline對齊
android:layout_alignBottom 將該控件的底部邊緣與給定ID控件的底部邊緣
android:layout_alignLeft 將該控件的左邊緣與給定ID控件的左邊緣對齊
android:layout_alignRight 將該控件的右邊緣與給定ID控件的右邊緣對齊
android:layout_alignTop 將給定控件的頂部邊緣與給定ID控件的頂部對齊
android:alignParentBottom 若是該值爲true,則將該控件的底部和父控件的底部對齊
android:layout_alignParentLeft 若是該值爲true,則將該控件的左邊與父控件的左邊對齊
android:layout_alignParentRight 若是該值爲true,則將該控件的右邊與父控件的右邊對齊
android:layout_alignParentTop 若是該值爲true,則將空間的頂部與父控件的頂部對齊
android:layout_centerHorizontal 若是值爲真,該控件將被至於水平方向的中央
android:layout_centerInParent 若是值爲真,該控件將被至於父控件水平方向和垂直方向的中央
android:layout_centerVertical 若是值爲真,該控件將被至於垂直方向的中央
-->
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="
@android :drawable/editbox_background"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
三、表單佈局 TableLayout
和TableRow配合使用,和HTML裏的Table類似。
舉個例子:
java代碼:
<?xml version="1.0" encoding="utf-8" ?>
<TableLayout xmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="fill_parent"android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="打開..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="保存..."
android:padding="3dip" />
<TextView
android:text="Ctrl-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="另存爲..."
android:padding="3dip" />
<TextView
android:text="Ctrl-Shift-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView android:text="*" android:padding="3dip" />
<TextView android:text="導入..." android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="*" android:padding="3dip" />
<TextView android:text="導出..." android:padding="3dip" />
<TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />
</TableRow>
<View android:layout_height="2dip" android:background="#FF909090" />
<TableRow>
<TextView android:layout_column="1" android:text="退出" android:padding="3dip" />
</TableRow>
</TableLayout>
四、切換卡 Tabwidget
繼承TabActivity,實現標籤的切換功能。
舉個例子:
java代碼:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android=" http://schemas.android.com/apk/res/android "
android:id="
@android :id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="
@android :id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="
@android :id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" /> <TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" /> <TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" /> </FrameLayout> </LinearLayout> </TabHost> 其餘佈局: 一、幀佈局 FrameLayout: 是最簡單的一個佈局對象。在他裏面的的全部顯示對象愛你過都將固定在屏幕的左上角,不能指 定位置,但容許有多個顯示對象,只是後一個會直接覆蓋在前一個之上顯示,會把前面的組件部 分或所有擋住。 舉個例子: java代碼: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="big" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50pt"/> <TextView android:text="middle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20pt"/> <TextView android:text="small" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt"/> </FrameLayout> 二、絕對佈局 AbsoluteLayout 絕對定位AbsoluteLayout,又能夠叫作座標佈局,能夠直接指定子元素的絕對位置,這種佈局簡 單直接,直觀性強,可是因爲手機屏幕尺寸差異比較大,使用絕對定位的適應性會比較差。分辨 率不同的屏幕,顯示的位置也會有所不一樣。 舉個例子: java代碼: < ?xml version="1.0" encoding="utf-8"?> < AbsoluteLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < EditText android:text="Welcome to Mr Wei's blog" android:layout_width="fill_parent" android:layout_height="wrap_content"/> < Button android:layout_x="250px" //設置按鈕的X座標 android:layout_y="40px" //設置按鈕的Y座標 android:layout_width="70px" //設置按鈕的寬度 android:layout_height="wrap_content" android:text="Button"/> < /AbsoluteLayout>