編輯:Android 開發實例
對剛接觸 Android 的人來說,界面布局往往是第一個燙手山芋。用 XML 做布局確實有點繁瑣,需要一段適應期。前陣子寫了一個五百多行的 View 布局,寫完雖然吐血,但對 Android 布局機制總算有了比較深刻的認識。
最常見的一種。多數情況下,把子元件的寬度或高度設為 fill_parent 與 wrap_content 的組合會比較合適。
最簡單的例子:兩個 TextView 水平排列,若前一個的寬度設為 wrap_content,系統會依它的實際寬度,緊接在其後方配置第二個。
還有一個實用小技巧:若要在兩個元件之間隔出一段固定距離,可以在中間塞一個沒有內容、只有寬度的 TextView 當墊片。
用 LinearLayout 做間隔時,往往需要大量空的 TextView 來墊位置,寫起來很累。這種情況就可以考慮改用 AbsoluteLayout。
它要求你明確給出每個元件的 x、y 座標,且座標是相對父容器的左上角。由於沒有自動排版,最好同時給出確定的寬與高,否則元件尺寸會變得難以預期。
缺點也很明顯:版面寫死了像素座標,換一個解析度不同的螢幕就會錯位。所以它只適合內部元件尺寸固定的局部區域,不適合當成整個畫面的骨架。
使用頻率較低,但有一個特性很實用:所有子元件都疊在父容器的左上角,呈現出互相覆蓋的效果。這正是實作「卡片疊放」「浮層覆蓋」「 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>
從這個結構可以歸納出幾個實務經驗:
View.VISIBLE/GONE 切換即可,效能與開發成本都更低。dip(dp)定義尺寸、用 sp 定義字級。layout_weight 來分配空間。寫完那五百行才真正體會到:布局不是「寫得出來就好」,而是要在可讀性、效能與跨螢幕適應性之間取捨。理解各布局的定位特性,比背誦屬性有用得多。
轉自:https://www.cnblogs.com/jk1001/archive/2010/07/24/1784109.html
Android MediaPlayer 完整指南:播放控制、進度條與快轉倒轉
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而 MediaPlayer 是最直覺的一個。它讓你以幾行程式碼就能控制內建媒體播放服務,完成播放、暫停、跳
Android手機衛士(十六):事件傳遞與 CheckBox 點擊衝突
當點擊 SettingItemView 時,事件會由父層配置接收,再傳遞給自訂元件。若點擊位置在 CheckBox,事件可能被 CheckBox 消耗,導致自訂元件無法更
Android 與 JSON 的對話:從字串到可用資料的四個步驟
JSON 的全名是 JavaScript Object Notation,格式源自 JavaScript 的物件寫法,但早已成為跨語言通用的資料交換標準。它比 XML 精
用 MediaPlayer 做一個 Android 音樂播放器
要在 Android 上播放音訊或影片,最直接的方式就是走 MediaPlayer。它位於 android.media 套件,能播放應用程式內的資源、裝置上的檔案,也能處