个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑

Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑:

此坑出现的条件:

1.RelativeLayout布局的layout_height属性设置为wrap_content

2.某“居中View”设置为layout_centerX(layout_centerVertical、layout_centerHorizontal、layout_centerInParent)

3.其它View相对于“居中View”做布局

此时坑出现了:

无论其它View设置了在“居中View”的下方(layout_below、layout_below)、上方(layout_above)、底部对齐(layout_alignBottom)等等,通通无效!

 

填坑方案:

RelativeLayout布局的layout_height属性设置为match_parent即可!

即使外层有LinearLayout使用layout_weight平分空间,它的match_parent也不会撑大外层LinearLayout的空间的。

当初想设置wrap_content,其实就是怕内部的RelativeLayout设置match_parent后会被撑大而已,实践证明,多虑了!

转载于:https://www.cnblogs.com/AsionTang/p/4029446.html

shu3.xml:<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context=".ShuHai3Activity"> <!-- Toolbar --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 播放视频区域 --> <!-- 间线和缩略图区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.55" android:background="@android:color/black" app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.6" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintVertical_bias="0.0"> <!-- 模拟萤石播放加载动画 --> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <!-- 视频播放View,可以替换成具体的视频播放控件 --> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.45" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 多个缩略图子项 --> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> 修改下面shuhaisp.xml布局和上面shu3.xml布局一样,保持shuhaisp.xml依赖不变必须使用android支持库。shuhaisp.xml:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- tools:context=".ShuhaiSp"--> <!-- <!– 顶部工具栏 –>--> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#44bd32"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/toolbar" android:layout_above="@+id/thumbnail_scrollview" android:layout_marginTop="0dp" android:background="@android:color/black"> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentBottom="true" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:rowCount="0" android:alignmentMode="alignBounds" android:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> --> </android.widget.GridLayout> </LinearLayout> </ScrollView> </RelativeLayout>
最新发布
06-25
<think>我们有两个布局文件:shu3.xml和shuhaisp.xml。目标是将shuhaisp.xml修改成和shu3.xml一样的布局,但必须使用Android支持库(即不能使用AndroidX)。同,保持shuhaisp.xml中已有的依赖不变。分析shu3.xml布局特点:1.根布局是ConstraintLayout。2.包含一个Toolbar(RelativeLayout),内部有一个返回按钮。3.视频播放区域(FrameLayout)位于Toolbar下方,并且上方约束在Toolbar底部,下方约束在缩略图区域顶部。同,其高度是父布局高度的55%(通过app:layout_constraintHeight_percent="0.55"实现)。4.缩略图区域(ScrollView)位于视频播放区域下方,高度为父布局的45%,并且底部约束在父布局底部。5.缩略图区域内部是一个GridLayout(来自android.support.v7.widget.GridLayout),设置为2列。而shuhaisp.xml当前布局:1.根布局RelativeLayout。2.同样有Toolbar(RelativeLayout)、视频播放区域(FrameLayout)和缩略图区域(ScrollView)。3.视频播放区域和缩略图区域使用RelativeLayout的相对定位(layout_below和layout_above)来放置。要求修改shuhaisp.xml,使用ConstraintLayout并实现与shu3.xml相同的布局结构。修改步骤:1.将根布局改为android.support.constraint.ConstraintLayout,并添加必要的命名空间。2.调整子视图的约束条件,使用ConstraintLayout的约束属性。3.视频播放区域的高度设置为父布局的55%,缩略图区域高度设置为45%。4.将GridLayout改为支持库版本(android.support.v7.widget.GridLayout)。注意:由于原shuhaisp.xml中使用了android.widget.GridLayout,现在需要替换为android.support.v7.widget.GridLayout。同,保留原布局中的其他属性(如背景、id等)。修改后的布局代码应该如下:</think>要将 `shuhaisp.xml` 布局修改为与 `shu3.xml` 相同的结构,同保持 Android 支持库依赖不变,我们需要做以下关键修改: ### 主要修改点: 1. **根布局**:将 `RelativeLayout` 改为 `android.support.constraint.ConstraintLayout` 2. **约束设置**:使用百分比约束控制视频区域和缩略图区域的高度比例 3. **GridLayout**:替换为支持库版本的 `android.support.v7.widget.GridLayout` 4. **布局参数**:添加约束布局的定位属性和百分比高度控制 ### 修改后的完整代码: ```xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 修改点1:根布局改为ConstraintLayout --> <!-- 顶部工具栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <!-- 添加约束定位 --> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="0dp" android:layout_height="0dp" android:background="@android:color/black" app:layout_constraintHeight_percent="0.55" <!-- 关键:高度占比55% --> app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar"> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintHeight_percent="0.45" <!-- 关键:高度占比45% --> app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 修改点:使用支持库的GridLayout --> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <ImageView android:layout_width="0dp" android:layout_height="wrap_content" app:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> --> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> ``` ### 关键修改说明: 1. **根布局转换**: ```xml <!-- 修改前 --> <RelativeLayout ...> ... </RelativeLayout> <!-- 修改后 --> <android.support.constraint.ConstraintLayout ...> ... </ConstraintLayout> ``` 2. **百分比高度控制**: ```xml <!-- 视频区域占55% --> app:layout_constraintHeight_percent="0.55" <!-- 缩略图区域占45% --> app:layout_constraintHeight_percent="0.45" ``` 3. **约束关系设置**: ```xml <!-- 视频区域约束 --> app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintTop_toBottomOf="@id/toolbar" <!-- 缩略图区域约束 --> app:layout_constraintTop_toBottomOf="@id/video_container" app:layout_constraintBottom_toBottomOf="parent" ``` 4. **GridLayout替换**: ```xml <!-- 修改前 --> <android.widget.GridLayout ...> ... </android.widget.GridLayout> <!-- 修改后 --> <android.support.v7.widget.GridLayout ...> ... </android.support.v7.widget.GridLayout> ``` ### 布局效果说明: 1. **层级结构**: ``` ConstraintLayout (根布局) ├── RelativeLayout (工具栏) ├── FrameLayout (视频容器,占55%高度) └── ScrollView (缩略图区域,占45%高度) └── GridLayout (支持库版本) ``` 2. **比例控制**: - 视频区域:屏幕高度的55% - 缩略图区域:屏幕高度的45% - 使用`layout_constraintHeight_percent`精确控制比例 3. **兼容性**: - 完全使用Android支持库组件 - 兼容旧版Android设备 ### 相关问题: 1. 如何在ConstraintLayout中实现百分比高度布局? 2. Android支持库的GridLayout与标准GridLayout有何区别? 3. 为什么在视频播放区域使用FrameLayout作为容器? 4. 如何优化缩略图GridLayout的加载性能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值