ConstraintLayout约束布局引入include加权重

本文介绍了在Android开发中遇到的ConstraintLayout使用问题,特别是关于include标签的使用及权重设置。在创建主页fragment并结合底部切换时,作者发现ConstraintLayout的include布局导致一些方法失效。解决方法是在include中添加宽高属性,之后权重与其他控件匹配,实现了页面元素的灵活布局。此文章作为开发过程中的经验记录,分享踩坑与解决方案。

引言:就是写主页fragment+底部切换时 发现的亿个小问题

首先说我对ConstraintLayout的理解

详细的地方,搜索全是,我这个理解是日常版本,然后是include的问题

我引入include后

app:layout_constraintBottom_toTopOf

这类方法全部失效了,然后搜索了一下,就是要加宽高

  <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/main_bottom_layout"
        layout="@layout/layout_bottom_bars" />

加入宽高后就可以了,左右摇摆了

然后就是权重和其他控件权重一样,懂得都懂

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/main_bottom_layout"
        app:layout_constraintVertical_weight="1"
        android:orientation="horizontal" />

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/main_bottom_layout"
        layout="@layout/layout_bottom_bars" />
</androidx.constraintlayout.widget.ConstraintLayout>

就是这样,出来的成品是

也没啥可说的,做个记录,记录一下踩的坑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值