如果我尝试以编程方式在 ConstraintLayout 中创建加权链,则不会显示任何 View 。在布局 xml 文件中创建它们工作正常。

我试图将 View 宽度设置为 MATCH_CONSTRAINT,但随后它们消失了。设置水平权重也不会改变任何东西。

这有效:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayoutTop"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:layout_constraintBottom_toTopOf="@+id/constraintLayoutBottom"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="1"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="2"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>

这不起作用:
ConstraintLayout bottomLayout = findViewById(R.id.constraintLayoutBottom);
TextView textView;
ConstraintSet set;

List<List<Integer>> rows = new ArrayList<>();
List<Integer> col1 = new ArrayList<>();
List<Integer> col2 = new ArrayList<>();

col1.add(1);
col1.add(2);
col2.add(3);
col2.add(4);

rows.add(col1);
rows.add(col2);

for (int y = 0; y < rows.size(); y++) {
    List<Integer> cols = rows.get(y);
    for (int x = 0; x < cols.size(); x++) {
        Integer i = cols.get(x);

        textView = new TextView(getApplicationContext());
        textView.setText(i.toString());
        textView.setId(i);

        //ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        //textView.setLayoutParams(params); // this makes views disappear!

        bottomLayout.addView(textView);

        int leftId = i == 1 || i == 3 ? ConstraintSet.PARENT_ID : i - 1;
        int topId = i == 1 || i == 2 ? ConstraintSet.PARENT_ID : i - 2;
        int rightId = i == 2 || i == 4 ? ConstraintSet.PARENT_ID : i + 1;
        int bottomId = i == 3 || i == 4 ? ConstraintSet.PARENT_ID : i + 2;

        set = new ConstraintSet();
        set.clone(bottomLayout);
        //set.constrainWidth(textView.getId(), ConstraintSet.MATCH_CONSTRAINT); // this makes views disappear!
        set.connect(textView.getId(), ConstraintSet.LEFT, leftId, ConstraintSet.RIGHT, 0);
        set.connect(textView.getId(), ConstraintSet.TOP, topId, ConstraintSet.BOTTOM, 0);
        set.connect(textView.getId(), ConstraintSet.RIGHT, rightId, ConstraintSet.LEFT, 0);
        set.connect(textView.getId(), ConstraintSet.BOTTOM, bottomId, ConstraintSet.TOP, 0);
        set.applyTo(bottomLayout);
    }
}

我如何在java中实现相同的目标?

最佳答案

我认为这里有一些问题需要解决。
例如,代码缺少链的创建(horizontalvertical ),您只需要在配置完成后应用 ConstraintSet() 一次。
我试图修复代码,它对我有用。

ConstraintLayout bottomLayout = findViewById(R.id.constraintLayoutBottom);
TextView textView;
ConstraintSet set = new ConstraintSet();

List<List<Integer>> rows = new ArrayList<>();
List<Integer> col1 = new ArrayList<>();
List<Integer> col2 = new ArrayList<>();

set.clone(bottomLayout);

col1.add(1);
col1.add(2);
col2.add(3);
col2.add(4);

rows.add(col1);
rows.add(col2);

for (int y = 0; y < rows.size(); y++) {
    List<Integer> cols = rows.get(y);
    for (int x = 0; x < cols.size(); x++) {
        Integer i = cols.get(x);

        textView = new TextView(getApplicationContext());
        textView.setText(i.toString());
        textView.setId(i);

        bottomLayout.addView(textView);

        int leftId = i == 1 || i == 3 ? ConstraintSet.PARENT_ID : i - 1;
        int topId = i == 1 || i == 2 ? ConstraintSet.PARENT_ID : i - 2;
        int rightId = i == 2 || i == 4 ? ConstraintSet.PARENT_ID : i + 1;
        int bottomId = i == 3 || i == 4 ? ConstraintSet.PARENT_ID : i + 2;

        set.connect(textView.getId(), ConstraintSet.LEFT, leftId, ConstraintSet.RIGHT, 0);
        set.connect(textView.getId(), ConstraintSet.TOP, topId, ConstraintSet.BOTTOM, 0);
        set.connect(textView.getId(), ConstraintSet.RIGHT, rightId, ConstraintSet.LEFT, 0);
        set.connect(textView.getId(), ConstraintSet.BOTTOM, bottomId, ConstraintSet.TOP, 0);
    }
}

set.createHorizontalChain(
    ConstraintSet.PARENT_ID,
    ConstraintSet.LEFT,
    ConstraintSet.PARENT_ID,
    ConstraintSet.RIGHT,
    new int[] {1, 2},
    null, // Here you can set a float array with the weights for your views
    ConstraintSet.CHAIN_SPREAD
)

set.createHorizontalChain(
    ConstraintSet.PARENT_ID,
    ConstraintSet.LEFT,
    ConstraintSet.PARENT_ID,
    ConstraintSet.RIGHT,
    new int[] {3, 4},
    null, // Here you can set a float array with the weights for your views
    ConstraintSet.CHAIN_SPREAD
)

set.createVerticalChain(
    ConstraintSet.PARENT_ID,
    ConstraintSet.TOP,
    ConstraintSet.PARENT_ID,
    ConstraintSet.BOTTOM,
    new int[] {1, 3},
    null, // Here you can set a float array with the weights for your views
    ConstraintSet.CHAIN_SPREAD
)

set.createVerticalChain(
    ConstraintSet.PARENT_ID,
    ConstraintSet.TOP,
    ConstraintSet.PARENT_ID,
    ConstraintSet.BOTTOM,
    new int[] {2, 4},
    null, // Here you can set a float array with the weights for your views
    ConstraintSet.CHAIN_SPREAD
)

set.applyTo(bottomLayout);

关于android - 以编程方式约束布局加权链,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48507442/

10-08 23:34