大家好,Android的介面主要靠布局檔案完成,只有掌握了其中的屬性與應用技藝,才能圓滿的完成任務,
所以今天我們來講講Android的布局。
自從htc1.0開始發布,安卓中有五大布局檔案,分別是:
LinearLayout 線性布局
RelativeLayout 相對布局
TableLayout 表格版面配置
FrameLayout 架構布局
AbsoluteLayout 絕對布局
首先布局檔案都有兩個最基本的屬性,寬和高 :
android:layout_width="fill_parent" android:layout_height="fill_parent"
fill_parent代表充滿整個螢幕,wrap_content代表適應整個螢幕,主要是適應自己控制項的大小,
從2.2開始加入match_parent這個屬性,也代表適應整個螢幕,但這個不僅適應自己控制項大小,也會把螢幕中的空白也佔一部分,使螢幕看起來沒那麼空曠,是個比較好用的屬性。但考慮到適配機型,開發人員不太熟悉,但提倡用。
——————————————————————————————————————————————-
下面是布局的詳細介紹:
線性布局:最普通的布局,預設子項目從左至右排列,有horizontal水平和vertical豎直排列這兩種屬性,一定要注意預設是水平排列。
相對布局:最好用、最常用的布局,可以使用其中的layout_alignParent和layout_toLeftOf、layout_above,再配上padding和margin等屬性(上一講有介紹),可以為所欲為的在螢幕中放控制項。
表格版面配置:主要在排版使用者名稱、密碼、愛好等這樣的格子時被使用,加入tableRow可以布局中jsp各種樣式的表格。
架構布局和絕對布局:前者最常用到的就是用在tabhost裡,以下是標準選項卡布局下的部分代碼
[html]
<?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"
android:background="@drawable/backgrounds"
android:orientation="vertical" >
<LinearLayout>
<FrameLayout android:id="@android:id/tabcontent"/>
<TabWidget android:id="@android:id/tabs"/>
</LinearLayout>
</TabHost>
<?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"
android:background="@drawable/backgrounds"
android:orientation="vertical" >
<LinearLayout>
<FrameLayout android:id="@android:id/tabcontent"/>
<TabWidget android:id="@android:id/tabs"/>
</LinearLayout>
</TabHost>但在另外一個場合,它也是必用布局,那就是疊加效果。
絕對布局主要是通過layout_x,layout_y來把所有的控制項定在螢幕絕對的位置,但由於其固執,難以改變,所以它在1.5以後被棄用,開發人員用FrameLayout中的scrollX,scrollY來定義部分控制項在布局中的絕對位置,可以說從而代替了絕對布局的功能。
還有其中一些布局如GridLayout進行圖文混排,ScrollView,用來載入大圖片,HorizonalScrollView用來載入水平方向大的圖片等簡單的布局。
今天布局先講到這兒,接下來繼續講述其他知識,其中的特性在其他章節我們會一會解釋,謝謝大家!