首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >RelativeLayout 根布局里有一个子布局预期一直展示,但子布局RelativeLayout被 覆盖了

RelativeLayout 根布局里有一个子布局预期一直展示,但子布局RelativeLayout被 覆盖了

原创
作者头像
lichong951
修改2025-12-03 14:47:57
修改2025-12-03 14:47:57
1440
举报

原因一句话

LinearLayout宽高都是match_parent,又在RelativeLayout默认不设置任何相对位置,于是它把父布局空间全部占满,之前靠align*margin定位的子控件自然被盖住。


解决思路(3选1)

1.让LinearLayout只占剩余区域,而不是全屏

给前面那个需要“一直展示”的控件一个id,然后让LinearLayout用剩余规则:

代码语言:xml
复制
<!--顶部固定条,始终可见-->
<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>

2.把LinearLayout改成wrap_content或固定值

如果你只是想弹个面板,就别让它match_parent

代码语言:xml
复制
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">

3.用layout_weight(嵌套一层LinearLayout)

顶层改成横向或纵向LinearLayout,把“始终展示”的view放weight=0,内容区放weight=1即可。


一句话总结

别让LinearLayout又满屏又不相对定位;给前面控件加id,再用layout_below/layout_above/layout_alignParentBottom等规则把空间切出来,就不会覆盖。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档