android之界面佈局


android裏有五大布局:


一、FrameLayout

幀佈局可以看成是牆腳堆東西,有一個四方的矩形的左上角牆腳,我們放了第一個東西,要再放一個,

那就在放在原來放的位置的上面,這樣依次的放,會蓋住原來的東西。

二、LinearLayout

線性佈局,每一個LinearLayout裏面又可分爲垂直佈局 (android:orientation="vertical")和水平佈局

(android:orientation="horizontal" )。當垂直佈局時,每一行就只有一個元素,多個元素依次垂直往下,

水平佈局時,只有一行,每一個元素依次向右排列。(linearLayout中有一個重要的屬性 android:layout_weight="1",

這個weight在垂直佈局時,代表行距;水平的時候代表列寬;weight值越大就越大)


三、AbsoluteLayout

絕對佈局,用X,Y座標來指定元素的位置,這種佈局方式也比較簡單,但是在垂直隨便切換時,往往會出問題,

而且多個元素的時候,計算比較麻煩。 


四、TableLayout

表格佈局類似Html裏面的Table。每一個TableLayout裏面有表格行TableRow,TableRow裏面可以具體定義每一個元素,

設定他的對齊方式 android:gravity="" 。在表格佈局中可以設置TableRow 可以設置 表格中每一行顯示的內容 以及位置 ,

可以設置顯示的縮進,對齊的方式。


五、RelativeLayout

相對佈局是android佈局中最爲強大的。相對佈局可以理解爲某一個元素爲參照物,來定位的佈局方式。在這裏簡單介紹:
//根據其他元素ID來確定位置
android:layout_above 將該控件的底部置於給定ID的控件之上;
android:layout_below 將該控件的底部置於給定ID的控件之下;
android:layout_toLeftOf 將該控件的右邊緣與給定ID的控件左邊緣對齊;
android:layout_toRightOf 將該控件的左邊緣與給定ID的控件右邊緣對齊;
// 居中
android:layout_centerHorizontal 如果爲true,將該控件的置於水平居中;
android:layout_centerVertical 如果爲true,將該控件的置於垂直居中;
android:layout_centerInParent 如果爲true,將該控件的置於父控件的中央;
// 指定移動像素
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;


佈局的嵌套

在界面佈局中用一個佈局往往不能很好地佈置界面,如果在佈局裏嵌套佈局,會更方便、也便於調整。

以在相對佈局中嵌套線性佈局爲例:

假如做一個音樂播放器的界面,我們要做一排按鈕,如果直接用相對佈局,按鈕會受其它元素影響,在這裏嵌套一個線性佈局

可使其穩定。


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout                                          <!--在這裏把線性佈局背景設爲紅色   -->
        
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:background="#FF66ff"
        android:layout_marginTop="300dip"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pre" 
            android:layout_marginTop="10dip"/>

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/play" 
            android:layout_marginLeft="5dip"
            />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pause" 
            android:layout_marginLeft="5dip"/>

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/reset" 
            android:layout_marginLeft="5dip"
            android:layout_marginTop="10dip"/>

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/next" 
            android:layout_marginLeft="5dip"
            android:layout_marginTop="10dip"/>
        
    </LinearLayout>

</RelativeLayout>



小知識:
關於android:layout_width="fill_parent" 和 android:layout_height="wrap_content" ,這是對每個佈局寬和高的設置。
 wrap_content 可表示隨着其中控件的不同而改變這個佈局的寬度或高度,類似於自動設置寬和高, fill_parent 使佈局填
充整個屏幕 。


小結:

我們可以根據不同的需要選擇不同的佈局,學會靈活運用。在這裏建議用相對佈局而不是絕對佈局,相對佈局屬性最多也是最強大一個,只要熟悉方法加以嵌套佈局會很好的佈置界面,而絕對佈局會出很多問題,比如垂直切換時的計算問題。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章