Android佈局是應用界面開發的重要一環,在Android中,共有五種佈局方式,分別是:LinearLayout (線性佈局),FrameLayout(框架布
局),AbsoluteLayout(絕對佈局),RelativeLayout(相對佈局),TableLayout(表格佈局)。javascript
在windows下有預覽功能,能夠在xml中查看佈局的樣式,在linux中無。java
1、LinearLayout
線性佈局,這個東西,從外框上能夠理解爲一個div,他首先是一個一個從上往下羅列在屏幕上。每個LinearLayout裏面又可分爲垂直佈局(android:orientation="vertical")和水平佈局(android:orientation="horizontal" )。當垂直佈局時,每一行就只有一個元素,多個元素依次垂直往下;水平佈局時,只有一行,每個元素依次向右排列。
linearLayout中有一個重要的屬性 android:layout_weight="1",這個weight在垂直佈局時,表明行距;水平的時候表明列寬;weight值越大就越大。linux
線形佈局中預覽和真機中徹底同樣。android
TextView佔必定的空間,沒有賦值也有必定的寬高,要特別注意。
2、FrameLayout
FrameLayout是最簡單的一個佈局對象。它被定製爲你屏幕上的一個空白備用區域,以後你能夠在其中填充一個單一對象 — 好比,一張你要發佈的圖片。全部的子元素將會固定在屏幕的左上角;你不能爲FrameLayout中的一個子元素指定一個位置。後一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或所有擋住(除非後一個子元素是透明的)。
3、AbsoluteLayout
AbsoluteLayout 這個佈局方式很簡單,主要屬性就兩個 layout_x 和 layout_y 分別定義 這個組件的絕對位置。 即,以屏幕左上角爲(0,0)的座標軸的x,y值,當向下或向右移動時,座標值將變大。AbsoluteLayout 沒有頁邊框,容許元素之間互相重疊(儘管不推薦)。咱們一般不推薦使用 AbsoluteLayout ,除非你有正當理由要使用它,由於它使界面代碼太過剛性,以致於在不一樣的設備上可能不能很好地工做。windows
4、RelativeLayout
相對佈局能夠理解爲某一個元素爲參照物,來定位的佈局方式。app
android:layout_方向 = id 表示 在這個id對應的控件的方向上(上|下)
android:layout_align方向 = id 表示和這個控件的(上下左右)對齊
android: layout_to方向Of = id 表示在這個控件的 左或者右
eg:框架
android:layout_below="@id/la1"/>
將當前控件放置於id爲la1 的控件下方。
android:layout_alignParentRight="true"
使當前控件的右端和父控件的右端對齊。這裏屬性值只能爲true或false,默認false。
android:layout_marginLeft="10dip"
使當前控件左邊空出相應的空間。
android:layout_toLeftOf="@id/true"
使當前控件置於id爲true的控件的左邊。
android:layout_alignTop="@id/ok"
使當前控件與id爲ok的控件上端對齊。
5、TableLayout
表格佈局相似Html裏面的Table。每個TableLayout裏面有表格行TableRow,TableRow裏面能夠具體定義每個元素。每一個TableRow 都會定義一個 row (事實上,你能夠定義其它的子對象,這在下面會解釋到)。TableLayout 容器不會顯示row 、cloumns 或cell 的邊框線。每一個 row 擁有0個或多個的cell ;每一個cell 擁有一個View 對象。表格由列和行組成許多的單元格。表格容許單元格爲空。單元格不能跨列,這與HTML 中的不同。ide
TabRow只論行,不論列(列自定義)。
每個佈局都有本身適合的方式,另外,這五個佈局元素能夠相互嵌套應用,作出美觀的界面。佈局
例子:this

1 線性佈局(LinearLayout)

描述:最簡單佈局方式,依次向下進行排列。
<?
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
="fill_parent"
>
<
Button
android:text
="Up"
android:id
="@+id/Button03"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
></
Button
>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<
Button
android:text
="left"
android:id
="@+id/Button01"
android:width
="120px"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
Button
>
<
Button
android:text
="right"
android:id
="@+id/Button02"
android:width
="120px"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
Button
>
</
LinearLayout
>
</
LinearLayout
>
二、 表格佈局(TableLayout)

描述:相似於HTML table ,在其中間添加View 或是<TableRow></TableRow>控件。
<?
xml version="1.0" encoding="utf-8"
?>
<
TableLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:id
="@+id/TableLayout01"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<
TableRow
android:gravity
="center"
>
<
Button
android:text
="@+id/Button01"
android:id
="@+id/Button01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</
Button
>
</
TableRow
>
<
TableRow
android:gravity
="center"
>
<
TextView
android:text
="第一行第0列"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
TextView
>
<
TextView
android:text
="第一行第1列"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
TextView
>
</
TableRow
>
<
TableRow
android:gravity
="center"
>
<
TextView
android:text
="第二行第0列"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
TextView
>
<
TextView
android:text
="第二行第1列"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
></
TextView
>
</
TableRow
>
</
TableLayout
>
三、 單幀佈局(FrameLayout)

描述:相似於HTML層疊
<?
xml version="1.0" encoding="utf-8"
?>
<
FrameLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
<
ImageView
android:id
="@+id/ImageView01"
android:src
="@drawable/circle_blue"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</
ImageView
>
<
ImageView
android:id
="@+id/ImageView02"
android:src
="@drawable/circle_green"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</
ImageView
>
<
ImageView
android:id
="@+id/ImageView03"
android:src
="@drawable/circle_red"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</
ImageView
>
</
FrameLayout
>
四、 相對佈局(RelativeLayout)

描述:取決於對參照控件進行佈局,父控件和子控件都可
經常使用屬性:android:layout_centerInParent=」true/false」
android:layout_above, android:layout_below
android:layout_alignleft, android:layout_alignright.
<?
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:id
="@+id/btnmiddle"
android:text
="MiddleButton"
android:layout_width
="200px"
android:layout_height
="wrap_content"
android:layout_centerInParent
="true"
>
</
Button
>
<
Button
android:id
="@+id/btnup"
android:text
="UpButton"
android:layout_width
="100px"
android:layout_height
="wrap_content"
android:layout_above
="@id/btnmiddle"
android:layout_alignLeft
="@id/btnmiddle"
>
</
Button
>
<
Button
android:id
="@+id/btndown"
android:text
="downButton"
android:layout_width
="100px"
android:layout_height
="wrap_content"
android:layout_below
="@id/btnmiddle"
android:layout_alignRight
="@id/btnmiddle"
>
</
Button
>
</
RelativeLayout
>
五、 座標佈局(AbsoluteLayout)

描述:對其控件進行直接定位,增長靈活性。
經常使用屬性:android:layout_x,android:layout_y.
<?
xml version="1.0" encoding="utf-8"
?>
<
AbsoluteLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
<
TextView
android:layout_width
="wrap_content"
android:text
="UserName:"
android:layout_height
="wrap_content"
android:id
="@+id/tvName"
android:layout_y
="20dip"
android:layout_x
="50dip"
>
</
TextView
>
<
TextView
android:layout_width
="wrap_content"
android:text
="Password:"
android:layout_height
="wrap_content"
android:id
="@+id/tvPassword"
android:layout_y
="100dip"
android:layout_x
="55dip"
>
</
TextView
>
<
EditText
android:layout_width
="150px"
android:layout_height
="wrap_content"
android:id
="@+id/tvPassword"
android:layout_y
="10dip"
android:layout_x
="120dip"
>
</
EditText
>
<
EditText
android:layout_width
="150px"
android:layout_height
="wrap_content"
android:id
="@+id/tvPassword"
android:layout_y
="90dip"
android:layout_x
="120dip"
>
</
EditText
>
</
AbsoluteLayout
>
MyLayout.java
package
com.jay.Layout;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public
class
MyLayout
extends
Activity {
/**
Called when the activity is first created.
*/
private
Button btnLinearlayout;
private
Button btnTablayout;
private
Button btnRelativelayout;
private
Button btnFramelayout;
private
Button btnAbsolutelayout;
OnClickListener listener;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
CreateControl();
listener
=
new
OnClickListener() {
@Override
public
void
onClick(View v) {
//
TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.btnlinearlayout:
setTitle(
"
線性佈局
"
);
setContentView(R.layout.linearlayout);
break
;
case
R.id.btntableayout:
setTitle(
"
表格佈局
"
);
setContentView(R.layout.tablelayout);
break
;
case
R.id.btnrelativelayout:
setTitle(
"
相對佈局
"
);
setContentView(R.layout.relativelayout);
break
;
case
R.id.btnfreamelayout:
setTitle(
"
單幀佈局
"
);
setContentView(R.layout.framelayout);
break
;
case
R.id.btnabsolutelayout:
setTitle(
"
座標佈局
"
);
setContentView(R.layout.absolutelayout);
break
;
}
}
};
btnLinearlayout.setOnClickListener(listener);
btnTablayout.setOnClickListener(listener);
btnRelativelayout.setOnClickListener(listener);
btnFramelayout.setOnClickListener(listener);
btnAbsolutelayout.setOnClickListener(listener);
}
private
void
CreateControl() {
//
TODO Auto-generated method stub
btnLinearlayout
=
(Button)findViewById(R.id.btnlinearlayout);
btnTablayout
=
(Button)findViewById(R.id.btntableayout);
btnRelativelayout
=
(Button)findViewById(R.id.btnrelativelayout);
btnFramelayout
=
(Button)findViewById(R.id.btnfreamelayout);
btnAbsolutelayout
=
(Button)findViewById(R.id.btnabsolutelayout); }}