我需要帮助ConstraintSet。我的目标是在代码中改变视图的约束,但我不知道如何做到这一点。

我有4个textview和一个ImageView。我需要将ImageView约束设置为textview之一。

check_answer4 = (TextView) findViewById(R.id.check_answer4);
check_answer1 = (TextView) findViewById(R.id.check_answer1);
check_answer2 = (TextView) findViewById(R.id.check_answer2);
check_answer3 = (TextView) findViewById(R.id.check_answer3);

correct_answer_icon = (ImageView) findViewById(R.id.correct_answer_icon);

如果第一个答案是正确的,我需要设置ImageView的约束为

app:layout_constraintRight_toRightOf="@+id/check_answer1"
app:layout_constraintTop_toTopOf="@+id/check_answer1"

如果第二个答案是正确的,我需要设置ImageView的约束为

app:layout_constraintRight_toRightOf="@+id/check_answer2"
app:layout_constraintTop_toTopOf="@+id/check_answer2"

等等。


当前回答

@vishakha yeolekar的解决方案对我不起作用。

要更改约束,我们需要遵循以下步骤:

克隆父布局 清除先前的约束 连接约束 将约束应用到父布局

解决方案代码(Kotlin)

val clParent = findViewById<ConstraintLayout>(R.id.parent_layout)
val mConstraintSet = ConstraintSet()
mConstraintSet.clone(clParent)
mConstraintSet.clear(R.id.imageView, ConstraintSet.END)
mConstraintSet.connect(R.id.imageView, ConstraintSet.END, R.id.check_answer, ConstraintSet.END)
mConstraintSet.applyTo(clParent)

这里是ConstraintSet的更多信息和方法的链接-点击这里。

其他回答

你也可以使用TransitionManager来动画化这些变化:

public static void animateConstraintLayout(ConstraintLayout constraintLayout, ConstraintSet set, long duration) {
    AutoTransition trans = new AutoTransition();
    trans.setDuration(duration);
    trans.setInterpolator(new AccelerateDecelerateInterpolator());

    TransitionManager.beginDelayedTransition(constraintLayout, trans);
    set.applyTo(constraintLayout);
}

你可以在用ConstraintSet更新布局后这样调用它:

ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.connect(R.id.example, ConstraintSet.BOTTOM, R.id.anotherExample, ConstraintSet.BOTTOM);
set.connect(R.id.example, ConstraintSet.TOP, R.id.anotherExample, ConstraintSet.TOP);

animateConstraintLayout(constraintLayout, set, 500);

我知道我的回答很晚了,但我相信它会帮助到其他经常来这里的人。 这篇文章不是我写的,但我做了一些修改,也就是说,你应该努力在这里查看完整的文章

约束集

在Java代码中使用约束集的关键是ConstraintSet类。这个类包含一系列的方法,这些方法允许执行诸如创建、配置和将约束应用到ConstraintLayout实例等任务。此外,ConstraintLayout实例的当前约束可以被复制到ConstraintSet对象中,并用于将相同的约束应用到其他布局(修改或不修改)。

ConstraintSet实例的创建就像其他Java对象一样:

ConstraintSet set = new ConstraintSet();

一旦创建了约束集,就可以在实例上调用方法来执行广泛的任务。 下面的代码配置了一个约束集,其中Button视图的左侧连接到EditText视图的右侧,边界为70dp:

set.connect(button1.getId(), ConstraintSet.LEFT, 
        editText1.getId(), ConstraintSet.RIGHT, 70);

将约束应用到布局中 一旦配置了约束集,它必须在ConstraintLayout实例生效之前应用到它。约束集是通过调用applyTo()方法来应用的,传递一个对要应用设置的布局对象的引用:

set.applyTo(myLayout);

还有很多东西你可以用ConstraintSet API,设置水平和垂直偏差,水平和垂直中心,操作链和更多。

读得真不错。

再说一次,这只是一种适应。

例如:

app:layout_constraintRight_toRightOf = "parentView"

如果你喜欢用程序声明它,它将是…

ConstraintLayout.LayoutParams.rightToRight = parentView.getId()

如果你使用程序来声明parentview(viewgroup),不要忘记给parentview一个生成的id。

parentView.setId(View.generatedId());

@vishakha yeolekar的解决方案对我不起作用。

要更改约束,我们需要遵循以下步骤:

克隆父布局 清除先前的约束 连接约束 将约束应用到父布局

解决方案代码(Kotlin)

val clParent = findViewById<ConstraintLayout>(R.id.parent_layout)
val mConstraintSet = ConstraintSet()
mConstraintSet.clone(clParent)
mConstraintSet.clear(R.id.imageView, ConstraintSet.END)
mConstraintSet.connect(R.id.imageView, ConstraintSet.END, R.id.check_answer, ConstraintSet.END)
mConstraintSet.applyTo(clParent)

这里是ConstraintSet的更多信息和方法的链接-点击这里。

在Kotlin中,您可以简单地扩展ConstraintSet类并添加一些方法来利用Kotlin中的dsl并生成更可读的代码。 像这样

class KotlinConstraintSet : ConstraintSet() {

    companion object {
        inline fun buildConstraintSet(block:KotlinConstraintSet.()->Unit) =
            KotlinConstraintSet().apply(block)
    }
    //add this if you plan on using the margin param in ConstraintSet.connect
    var margin: Int? = null
        get() {
            val result = field
            margin = null //reset it to work with other constraints
            return result
        }

    inline infix fun Unit.and(other: Int) = other // just to join two functions

    inline infix fun Int.topToBottomOf(bottom: Int) =
        margin?.let {
            connect(this, TOP, bottom, BOTTOM, it)
        } ?: connect(this, TOP, bottom, BOTTOM)

    inline fun margin(margin: Int) {
        this.margin = margin
    }

    inline infix fun Int.bottomToBottomOf(bottom: Int) =
        margin?.let {
            connect(this, BOTTOM, bottom, BOTTOM, it)
        } ?: connect(this, BOTTOM, bottom, BOTTOM)

    inline infix fun Int.topToTopOf(top: Int) =
        margin?.let {
            connect(this, TOP, top, TOP, it)
        } ?: connect(this, TOP, top, TOP)

    inline infix fun Int.startToEndOf(end: Int) =
        margin?.let {
            connect(this, START, end, END, it)
        } ?: connect(this, START, end, END)

            ...
    //TODO generate other functions depending on your needs

    infix fun Int.clear(constraint: Constraints) =
        when (constraint) {
            Constraints.TOP -> clear(this, TOP)
            Constraints.BOTTOM -> clear(this, BOTTOM)
            Constraints.END -> clear(this, END)
            Constraints.START -> clear(this, START)
        }

    //inline infix fun clearTopCon
    inline infix fun appliesTo(constraintLayout: ConstraintLayout) =
        applyTo(constraintLayout)

    inline infix fun clones(constraintLayout: ConstraintLayout) =
        clone(constraintLayout)

    inline fun constraint(view: Int, block: Int.() -> Unit) =
        view.apply(block)
}

enum class Constraints {
    TOP, BOTTOM, START, END //you could add other values to use with the clear fun like LEFT
}

像这样使用它

        buildConstraintSet {
            this clones yourConstraintLayout
            constraint(R.id.view1) {
                margin(value:Int) and this topToBottomOf R.id.view2
                margin(30) and this bottomToBottomOf ConstraintSet.PARENT_ID
            }
            constraint(R.id.view2) {
                this clear Constraints.BOTTOM
                margin(0) and this topToTopOf R.id.topGuide
            }
            constraint(R.id.view4) {
                this topToTopOf R.id.view2
                this bottomToBottomOf R.id.view3
                this startToEndOf R.id.view2
            }
            //or you could simply do
            R.id.view1 startToEndOf R.view2
            R.id.view1 toptToBottomOf R.view3
            R.id.view3 bottomtToBottomOf R.view2
            R.id.view3 clear Constraints.END

            // and finally call applyTo()
            this appliesTo yourConstraintLayout
        }