經典需求:頂部固定高度標題欄、底部固定高度底部欄、中間 FrameLayout 自動填滿剩餘高度。
layout_weight="1" + layout_height="0dp"<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 頂部標題欄:固定高度 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?attr/colorPrimary"
android:gravity="center_vertical"
android:paddingHorizontal="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標題"
android:textSize="20sp"
android:textColor="@android:color/white"/>
</LinearLayout>
<!-- 中間內容區:佔據剩餘空間 -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- 底部欄:固定高度 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/white"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="底部資訊" />
</LinearLayout>
</LinearLayout>
LinearLayout 垂直排列,總高 match_parent。layout_height(如 56dp、48dp)。FrameLayout 設 layout_height="0dp" 並 layout_weight="1",告訴佈局系統:「先分配固定高度給兄弟元素,剩餘空間全給我」。同理可用 ConstraintLayout 配合 app:layout_constraintTop_toBottomOf/Bottom_toTopOf 實現,效能更優、層級更淺。