原因一句話 LinearLayout 寬高都是 match_parent,又在 RelativeLayout 裏 默認不設置任何相對位置,於是它把父佈局空間全部佔滿,之前靠 align* 或 margin 定位的子控件自然被蓋住。
解決思路(3 選 1)
- 讓 LinearLayout 只佔剩餘區域,而不是全屏 給前面那個需要“一直展示”的控件一個 id,然後讓 LinearLayout 用剩餘規則:
<!-- 頂部固定條,始終可見 -->
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="標題欄"
android:gravity="center"
android:background="@color/colorPrimary"/>
<!-- 內容區,只佔剩餘 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/title" <!-- 關鍵:在標題下方 -->
android:orientation="vertical">
<!-- 你的內容 -->
</LinearLayout>
- 把 LinearLayout 改成 wrap_content 或固定值 如果你只是想彈個面板,就別讓它
match_parent:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
- 用
layout_weight(嵌套一層 LinearLayout) 頂層改成橫向或縱向LinearLayout,把“始終展示”的 view 放weight=0,內容區放weight=1即可。
一句話總結 別讓 LinearLayout 又滿屏又不相對定位;給前面控件加 id,再用 layout_below / layout_above / layout_alignParentBottom 等規則把空間切出來,就不會覆蓋。