Android开发:如何将一个子LinearLayout的高度设置为另一个子LinearLayout的高度?
嗨,这个问题其实很好解决,我给你两种常用的方案,看你习惯用哪种布局~
解决方案一:用LinearLayout原生实现(简单直接)
如果你的父布局本来就是LinearLayout,只需要调整第二个子布局的android:layout_height属性就行:
- 父LinearLayout保持
android:layout_height="wrap_content",它的高度会被内部最高的子布局(也就是第一个带4个TextView的子布局)撑开 - 把第二个子布局的
android:layout_height从wrap_content改成match_parent,这样它就会自动匹配父布局的高度,也就是和第一个子布局等高
完整代码示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 第一个子LinearLayout --> <LinearLayout android:id="@+id/first_child" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:text="文本1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本3" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本4" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <!-- 第二个子LinearLayout --> <LinearLayout android:id="@+id/second_child" android:layout_width="0dp" android:layout_height="match_parent" <!-- 关键:匹配父布局高度 --> android:layout_weight="1" android:orientation="vertical"> <TextView android:text="文本A" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本B" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
解决方案二:用ConstraintLayout实现(更灵活的现代布局方式)
如果你的项目已经在使用ConstraintLayout(现在Android官方更推荐这种布局),可以通过约束直接让两个子布局等高,这种方式扩展性更强,适合后续布局有更复杂需求的情况:
- 把父布局改成ConstraintLayout
- 给第一个子布局设置约束,让它在父布局中定位
- 给第二个子布局设置
app:layout_constraintHeight_equalTo="@id/first_child",强制它和第一个子布局高度一致,同时把android:layout_height设为0dp让约束生效
完整代码示例:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 第一个子LinearLayout --> <LinearLayout android:id="@+id/first_child" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/second_child" app:layout_constraintHorizontal_weight="1"> <TextView android:text="文本1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本3" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本4" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <!-- 第二个子LinearLayout --> <LinearLayout android:id="@+id/second_child" android:layout_width="0dp" android:layout_height="0dp" <!-- 让约束控制高度 --> android:orientation="vertical" app:layout_constraintStart_toEndOf="@id/first_child" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_weight="1" app:layout_constraintHeight_equalTo="@id/first_child"> <!-- 关键:和第一个子布局等高 --> <TextView android:text="文本A" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文本B" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
两种方案都能实现你要的效果,选哪种看你的项目布局习惯就好~
内容的提问来源于stack exchange,提问作者Om Prakash Gupta
相关产品推荐
相关产品推荐

