ConstraintLayout 实现LinearLayout weight效果

一:水平方向

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

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

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

1
layout_constraintRight_toLeftOf=“@+id/view1”

具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 <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>

二:垂直方向

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

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

layout_constraintTop_toBottomOf=view1的ID和 layout_constraintBottom_toTopOf=view2的ID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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一点效果都没有,而且你一般情况还找不到啥错误,盲人摸象……