Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 程式碼故事 第一回:那些等間距排列的按鈕

編輯:關於 Android 開發

App 新做了一個放操作按鈕的畫面,老闆的要求是「簡潔美觀有內涵,按鈕要均勻分布」。設計 MM 給了一張圖,示意起來大概是這樣:

|==============================================|
|==========[Button]==========[Button]==========|
|==========[Button]==========[Button]==========|
|==============================================|

當然設計稿給的是高清圖,上面只是示意。把需求翻譯成人話:兩個按鈕排在同一列,按鈕之間的間距、按鈕與螢幕邊緣的間距要相等,並且撐滿整個螢幕寬度。

直覺解法:用 layout_weight 切三等分

利用 LinearLayout 的權重特性,插入兩個 0dp 寬的占位 View,讓三段空間等寬:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <ImageView android:layout_width="40dp" android:layout_height="wrap_content" android:src="@drawable/record_start" android:gravity="center_horizontal"/> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <ImageView android:layout_width="40dp" android:layout_height="wrap_content" android:src="@drawable/game" android:gravity="center_horizontal" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </RelativeLayout> Android Layout XML

這裡有個容易忽略的細節:占位 View 的寬度必須寫 0dp 而不是 wrap_content。寫 0dp 時 weight 會把剩餘空間完整分配給它;寫 wrap_content 則會先量測內容再分,比例就會失真。

效果對比:權重比例怎麼抓

下面把兩種寫法放在一起比。layout1 用 5 : 13 : 5 的比例,layout2 用 1 : 1 : 1 加上 wrap_content 的按鈕:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="60dp" android:layout_centerVertical="true"> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#ffaa00" android:layout_weight="5"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:drawableTop="@drawable/ic_action_start" android:gravity="center_horizontal" android:layout_weight="13" android:background="#00ff80" android:text="Play again"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:drawableTop="@drawable/ic_action_stop" android:gravity="center_horizontal" android:layout_weight="13" android:background="#4490a0" android:text="Stop"/> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#ffaa00" android:layout_weight="5"/> </LinearLayout> <LinearLayout android:id="@+id/layout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/layout1" android:layout_centerVertical="true"> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_action_start" android:gravity="center_horizontal" android:text="Play"/> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_action_stop" android:gravity="center_horizontal" android:text="Stop"/> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </RelativeLayout> Android Layout XML

效果對比:

看起來解決了,但真的解決了嗎

問題表面上解決了,可以交差。但 Android 碎片化是繞不開的現實——螢幕寬度並不一定是 360dp,以為是這樣會被各種奇葩機型教育。這也正是不能用寫死 margin 的原因:寫死一個 20dp 間距,在 320dp 的機子上佔比過大,在 480dp 的平板上又會散開。

那把權重比例代入不同螢幕寬度算算看。取 W = [300, 400] 這段區間:

其中 L/2 是 weight=5 的實際寬度,L + w 是 weight=13 的實際寬度。「偏差」那一欄代表按鈕與螢幕邊緣的間距減去兩個按鈕之間的間距,它反映的就是三等分的精確程度。可以看到即使在最窄的 300dp 螢幕上,偏差也只有 3dp 左右——肉眼幾乎看不出來。

所以結論是:真的可以去交差了。

為什麼這就算完成了

工程決策很少是「完美」與「不完美」的二分,而是「在有限時間內達到夠好的解」。這個間距不是生死功能,用三年的程式碼與時間換到次優解,是划算的。硬要追求像素級精確,就得自己量螢幕寬度、動態計算每一段的 dp 值,程式碼複雜度與出錯風險都會上升,而收益是使用者根本感知不到的 1dp。

意外收穫

後來發現一個附加好處:當其中一個按鈕的 visibility 設成 GONE 時,因為 GONE 完全不佔版面配置空間,權重會重新分配,剩下的那顆按鈕剛好會自動置中顯示。不需要再寫額外邏輯處理單按鈕的排版,這是權重方案附贈的。

順帶一提,如果按鈕數量會動態變化、或需要橫向捲動,那麼 LinearLayout 加權重就不再適用,該換成 ConstraintLayout 的 chains(鏈)或 Flow 元件。鏈的 spread 模式天生就是做等間距排列的,而且能設定絕對間距,比權重更直觀。

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