In XML
         
         
          创建一个水平链,然后使用
          
           app:layout_constraintHorizontal_weight
          
          属性。
         
         <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent">
    <TextView
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#caf"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/two"
        app:layout_constraintHorizontal_weight="6"/>
    <TextView
        android:id="@+id/two"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#fac"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="4"/>
</android.support.constraint.ConstraintLayout>
In Java
创建你的视图并将它们添加到父类的ConstraintLayout。你将需要给他们每个人一个id,以便一切工作;你可以使用View.generateViewId()或者你可以为他们定义一个id资源。
// this will be MATCH_CONSTRAINTS width and 48dp height
int height = (int) (getResources().getDisplayMetrics().density * 48);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(0, height);
View left = new View(this);
left.setId(R.id.one);
parent.addView(left, params);
View right = new View(this);
right.setId(R.id.two);
parent.addView(right, params);
然后创建一个ConstraintSet对象并创建你的链。
ConstraintSet set = new ConstraintSet();
set.clone(parent);
int[] chainIds = { R.id.one, R.id.two }; // the ids you set on your views above
float[] weights = { 6, 4 };
set.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
                          ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
                          chainIds, weights, ConstraintSet.CHAIN_SPREAD);
set.applyTo(parent);