剛開始學習Android,一定要熟悉Android獨特的框架,今天我們要介紹的就是它的佈局文件。很多大俠看來很簡單,請一笑而過,謝謝~~
Android佈局有五種佈局方式,這是Android面試經常問到的問題。我就曾經面試時被問到,流利的說出來這五種佈局,肯定會留給面試官一個好印象,知道你還是瞭解Android開發的。五種佈局就是:
1.LinearLayout 流線佈局
2.FrameLayout幀佈局
3.AbsoluteLayout絕對佈局
4.RelativeLayout相對佈局
5.TableLayout表格佈局
一.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"
>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red"
android:background="#aa0000"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
<TextView android:text="green"
android:background="#00aa00"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_weight="1"></TextView>
<TextView android:text="green"
android:background="#00aa00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_weight="1"></TextView>
</LinearLayout>
</LinearLayout>
這是兩個LinearLayout標籤,嵌套在一個大的LinearLayout中,效果圖爲:
二 FrameLayout幀佈局,又稱爲層佈局,就是後面的組件覆蓋前面的組件。在某固定的位置顯示單一的對象,一層覆蓋一層,後面添加的組件覆蓋先添加的組件。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/untitled"></View>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Frame Layout13811346744"
android:padding="5px"
></TextView>
</FrameLayout>
其中untitled爲放到resource中的圖片文件名。美女圖片在爲第一層在下面,TextView爲第二層在上面,以此類推。
三.AbsoluteLayout絕對佈局,(0,0)爲手機屏幕左上角, 每一個元素都固定好具體的座標,這種方式不推薦,因爲每款手機的屏幕像素可能存在差異,一次會出現應用在不同手機上面顯示效果差異較大,這是我們作爲開發人員不願意看到的,不推薦使用,就不做詳細的介紹了。
四.RelativeLayout相對佈局,要學習相對佈局,得先學習相對佈局特有的屬性。
- (1 )layout_alignParentBottom 當前控件低端與父控件的低端對齊(重合)
- (2)layout_alignParentLeft 當前控件左端與父控件的左端對齊(重合)
- (3)layout_alignParentRight 當前控件右端與父控件的右端對齊(重合)
- (4)layout_alignParentTop 當前控件上端與父控件的上端對齊(重合)
- (5)layout_centerHorizontal 當前控件位於父控件的橫向中間位置(水平方向上的中間)
- (6)layout_centerInParent 當前控件位於父控件的縱橫向中間位置(垂直方向上的中間)
- (7)layout_centerVertical 當前控件位於父控件的縱向中間位置(平面上的正中間)
- (8)layout_above 使當前控件位於給出id控件的上方
- (9)layout_below 使當前控件位於給出id控件的下方
- (10)layout_toLeftOf 使當前控件位於給出id控件的左側
- (11)layout_toRightOf 使當前控件位於給出id控件的右側
- (12)layout_alignBottom 使當前控件與給出id控件的底部部重合(注意可用和給出id控件來對齊)
- (13)layout_alignLeft 使當前控件與給出id控件的左邊重合
- (14)layout_alignRight 使當前控件與給出id控件的右邊重合
- (15)layout_alignTop 使當前控件與給出id控件的頂部重合
- (16)layout_alignBaseline 使當前控件的BaseLine與給出id控件t的BaseLine重合,這個主要用於Label或者其他包含文本的widgets。
<?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="wrap_content"
android:padding="15px"
>
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textview1"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:text="textview2"/>
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100px"
android:text="textview3"
/>
</RelativeLayout>
五.TableLayout表格佈局, 將子元素的位置分配到行與列中,跟html中的table標籤類似,每個TableRow都會定義一個row
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView android:text="textview1"/>
<TextView android:text="textview2"
android:paddingLeft="100px"/>
</TableRow>
<TableRow>
<TextView android:text="textview3"/>
<TextView android:text="textview4"
android:paddingLeft="100px"/>
</TableRow>
<TableRow>
<TextView/>
<TextView android:text="textview5"
android:paddingLeft="100px"/>
</TableRow>
</TableLayout>
終於介紹完了,當然在項目中沒有例子這麼簡單,但是複雜的事情都是由每一個簡單的事情組合成的,這些佈局並不是死的,他們之間可以相互嵌套,組合出不同的變化,形成一個個交互性超強的activity。希望對初學者有所幫助。
leoo
2011/11/3