Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 界面布局入門:LinearLayout、AbsoluteLayout 與 FrameLayout

編輯:Android 開發實例

對剛接觸 Android 的人來說,界面布局往往是第一個燙手山芋。用 XML 做布局確實有點繁瑣,需要一段適應期。前陣子寫了一個五百多行的 View 布局,寫完雖然吐血,但對 Android 布局機制總算有了比較深刻的認識。

一、LinearLayout(線性布局)

最常見的一種。多數情況下,把子元件的寬度或高度設為 fill_parent 與 wrap_content 的組合會比較合適。

最簡單的例子:兩個 TextView 水平排列,若前一個的寬度設為 wrap_content,系統會依它的實際寬度,緊接在其後方配置第二個。

還有一個實用小技巧:若要在兩個元件之間隔出一段固定距離,可以在中間塞一個沒有內容、只有寬度的 TextView 當墊片。

二、AbsoluteLayout(絕對布局)

用 LinearLayout 做間隔時,往往需要大量空的 TextView 來墊位置,寫起來很累。這種情況就可以考慮改用 AbsoluteLayout。

它要求你明確給出每個元件的 x、y 座標,且座標是相對父容器的左上角。由於沒有自動排版,最好同時給出確定的寬與高,否則元件尺寸會變得難以預期。

缺點也很明顯:版面寫死了像素座標,換一個解析度不同的螢幕就會錯位。所以它只適合內部元件尺寸固定的局部區域,不適合當成整個畫面的骨架。

三、FrameLayout(框架布局)

使用頻率較低,但有一個特性很實用:所有子元件都疊在父容器的左上角,呈現出互相覆蓋的效果。這正是實作「卡片疊放」「浮層覆蓋」「 Tab 內容切換」這類需求的首選。

四、實例:一個下單頁面的布局結構

以下是一個購物頁的布局,刻意混用了三種方式,可以看出它們各自的適用位置。最外層是 LinearLayout,內部用 FrameLayout 承載四個可切換的畫面(商店資訊、商品列表、購物車、結帳),每個畫面內部再用 AbsoluteLayout 精細定位:

 1 <?xml version="1.0" encoding="utf-8"?>
 2
 3 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 4     android:layout_width="fill_parent" android:layout_height="fill_parent"
 5     android:background="@drawable/shop_back">
 6
 7     <LinearLayout
 8         android:layout_width="fill_parent" android:layout_height="260px"
 9         android:orientation="horizontal">
10
11         <!-- 以 FrameLayout 承載四個可切換的畫面 -->
12         <FrameLayout
13             android:layout_width="400px" android:layout_height="fill_parent"
14             android:background="@drawable/frame_border">
15
16             <!-- 畫面一:商店資訊 -->
17             <LinearLayout android:id="@+id/layout1"
18                 android:layout_width="fill_parent" android:layout_height="fill_parent"
19                 android:orientation="vertical">
20                 <AbsoluteLayout ...>
21                     <!-- 歡迎光臨、店名、公告、可捲動的公告內容 -->
22                     <ScrollView android:layout_x="25px" android:layout_y="125px"
23                         android:layout_width="332px" android:layout_height="90px">
24                         <TextView android:id="@+id/shopinfo_content" ... />
25                     </ScrollView>
26                 </AbsoluteLayout>
27             </LinearLayout>
28
29             <!-- 畫面二:商品列表 -->
30             <LinearLayout android:id="@+id/layout2" ...>
31                 <LinearLayout android:id="@+id/listview_layout" ...>
32                     <!-- 標題圖 + 商品名稱/價格標頭 + ListView -->
33                     <ListView android:id="@+id/product_list"
34                         android:layout_x="10px" android:layout_y="30px"
35                         android:layout_width="140px" android:layout_height="150px" />
36                 </LinearLayout>
37                 <LinearLayout android:id="@+id/product_layout" ...>
38                     <!-- 上一頁 / 商品圖 / 下一頁 / 名稱價格類型特點 -->
39                     <Button android:id="@+id/page_later" ... />
40                     <ImageView android:id="@+id/image" ... />
41                     <Button android:id="@+id/page_next" ... />
42                     <!-- 詳細資訊區 + 購買數量輸入 + 加入購物車 -->
43                     <EditText android:id="@+id/buy_number"
44                         android:layout_width="30px" android:layout_height="30px"
45                         android:text="1" />
46                     <Button android:id="@+id/button_addshoppingcart" ... />
47                 </LinearLayout>
48             </LinearLayout>
49
50             <!-- 畫面三:購物車 -->
51             <LinearLayout android:id="@+id/layout3" ...>
52                 <!-- 商品名稱/單價/數量/合計 標頭 + ListView + 結帳按鈕 -->
53                 <ListView android:id="@+id/shoppingcart_list"
54                     android:layout_width="340px" android:layout_height="150px" />
55                 <Button android:id="@+id/button_buy" ... />
56             </LinearLayout>
57
58             <!-- 畫面四:填寫訂單 -->
59             <LinearLayout android:id="@+id/layout4" ...>
60                 <TextView android:id="@+id/totalpay" ... />
61                 <EditText android:id="@+id/user_address"  ... />
62                 <EditText android:id="@+id/user_zipcode"  ... />
63                 <EditText android:id="@+id/user_phone"    ... />
64                 <AbsoluteLayout ...>
65                     <Button android:id="@+id/button_pay" ... />
66                     <Button android:id="@+id/button_cancelbuy" ... />
67                 </AbsoluteLayout>
68             </LinearLayout>
69
70         </FrameLayout>
71
72         <!-- 右側功能按鈕欄,用 AbsoluteLayout 精細定位 -->
73         <AbsoluteLayout
74             android:layout_width="wrap_content" android:layout_height="fill_parent">
75             <Button android:id="@+id/button_shopinfo"
76                 android:layout_x="0px" android:layout_y="30px" ... />
77             <Button android:id="@+id/button_productlist"
78                 android:layout_x="0px" android:layout_y="80px" ... />
79             <Button android:id="@+id/button_shoppingcart"
80                 android:layout_x="0px" android:layout_y="130px" ... />
81         </AbsoluteLayout>
82
83     </LinearLayout>
84 </LinearLayout>

從這個結構可以歸納出幾個實務經驗:

  • 外層用 LinearLayout 撐骨架,FrameLayer 做畫面切換,內部局部用 AbsoluteLayout 微調。三者分工,比試圖用單一種布局解決所有問題輕鬆得多。
  • 畫面切換不必真的建立四個 Activity。把四個 LinearLayout 疊在同一個 FrameLayout 裡,透過控制 View.VISIBLE/GONE 切換即可,效能與開發成本都更低。
  • 大量使用 px 是這份布局最大的問題。px 是實體像素,在不同密度螢幕上會嚴重失真;正式專案應改用 dip(dp)定義尺寸、用 sp 定義字級。
  • 能改用 RelativeLayout 或 ConstraintLayout 就改。本例中大量的 AbsoluteLayout 在後續版本已被標記為 deprecated,現代的相對/約束布局能同時兼顧彈性與效能,也讓 XML 更易讀。
  • 空的 TextView 墊片是權宜之計。更好的做法是使用 margin/padding,或直接在 LinearLayout 上設定 layout_weight 來分配空間。

寫完那五百行才真正體會到:布局不是「寫得出來就好」,而是要在可讀性、效能與跨螢幕適應性之間取捨。理解各布局的定位特性,比背誦屬性有用得多。

轉自:https://www.cnblogs.com/jk1001/archive/2010/07/24/1784109.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved