固定像素(`dp`/`px`)在不同密度螢幕上物理尺寸不同,導致元素超出可視區或留白過大。
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 頂部固定高度 -->
<View android:layout_width="match_parent"
android:layout_height="56dp" />
<!-- 中間列表佔剩餘空間 70% -->
<RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7" />
<!-- 底部操作欄佔 30% -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="確定" />
</LinearLayout>
</LinearLayout>
| 技術 | 適用場景 | 核心 API |
|---|---|---|
| 最小寬度限定符 | 針對 320dp/360dp/411dp/600dp/720dp 等斷點提供不同佈局 | `res/layout-sw360dp/` `res/layout-sw600dp/` |
| 百分比佈局 | 更直觀的百分比屬性(已棄用,建議用 ConstraintLayout + Guideline) | `PercentRelativeLayout` / `ConstraintLayout` `app:layout_constraintWidth_percent` |
| ConstraintLayout 鏈 | 複雜相對定位、鏈式分佈、佔比 | `app:layout_constraintHorizontal_chainStyle="spread_inside"` |
| VectorDrawable / WebP | 圖標、插圖無損縮放 | `android:src="@drawable/ic_vector"` |
| 字體縮放 sp | 文字跟隨系統字體大小設置 | `android:textSize="16sp"` |
原最佳回答「利用線性佈局特性,設置比例 weight 就可以解決」方向正確,上文提供完整 XML 範例、關鍵規則、進階組合拳表供直接落地。