如何在Android单个Activity中实现3个布局各占1/3垂直高度
垂直LinearLayout等分高度嵌套多布局实现
要实现三个子布局各占总高度1/3的效果,利用LinearLayout的权重分配特性即可完成,实现方式如下:
- 给外层垂直LinearLayout设置
android:weightSum="3",声明总权重为3 - 三个子布局统一配置
android:layout_height="0dp"+android:layout_weight="1",即可各自分配1/3的总高度
完整的布局示例代码如下:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="3"> <!-- 第一个ConstraintLayout子布局 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- 内部自定义内容 --> </androidx.constraintlayout.widget.ConstraintLayout> <!-- RelativeLayout子布局 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- 内部自定义内容 --> </RelativeLayout> <!-- 第二个ConstraintLayout子布局 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- 内部自定义内容 --> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>
注意:如果你使用的是非AndroidX的老版本支持库,把ConstraintLayout的类路径替换为
android.support.constraint.ConstraintLayout即可正常使用。
内容的提问来源于stack exchange,提问作者yoan_gauthier
相关产品推荐
相关产品推荐

