原因一句话
LinearLayout 宽高都是
match_parent,又在
RelativeLayout 里
默认不设置任何相对位置,于是它把父布局空间全部占满,之前靠
align* 或
margin 定位的子控件自然被盖住。
解决思路(3 选 1)
给前面那个需要“一直展示”的控件一个 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>
如果你只是想弹个面板,就别让它
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 等规则把空间切出来,就不会覆盖。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048520/viewspace-3104139/ ,如需转载,请注明出处,否则将追究法律责任。