ConstraintLayout 实现LinearLayout weight效果

一:水平方向

水平方向需要设置步骤如下

  1.  设置view的layoutwidth=0dp,id分别为view1,view2
  2. 分别view1 view2的
    app:layout_constraintHorizontal_weight="1"
  3. 分别设置view1与view2的左右依赖 
  4. view1 需要设置

    layout_constraintLeft_toRightOf="@+id/view2"
  5. view2需要设置依赖 

layout_constraintRight_toLeftOf=“@+id/view1”

具体代码如下

 <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constrain_bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/bg_top_cornel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/map_view"
        app:layout_constraintVertical_weight="2">

        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#ff0000"
            app:layout_constraintHorizontal_weight="2"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/view2"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/view2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            app:layout_constraintHorizontal_weight="3"
            app:layout_constraintLeft_toRightOf="@id/view1"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

二:垂直方向

与上面类似,不需要需要设置的属性如下:

layout_constraintVertical_weight="xxx",另外需要设置垂直方向的约束,尤其是

layout_constraintTop_toBottomOf=view1的ID和 layout_constraintBottom_toTopOf=view2的ID

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".activity.maintain.MapMaintainLocationActivity">

    <com.amap.api.maps.TextureMapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/constrain_bottom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="3" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constrain_bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/bg_top_cornel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/map_view"
        app:layout_constraintVertical_weight="2">

        <Button
            style="@style/button_round"
            android:layout_marginBottom="20dp"
            android:text="确定"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

总结:在进行weight属性设置时,一定不要忘记设置想要瓜分weight的几个view之间的水平或垂直约束,否则设置的weight一点效果都没有,而且你一般情况还找不到啥错误,盲人摸象……

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值