kotlin 我不能正确地给予编程约束

js4nwp54  于 2022-11-16  发布在  Kotlin
关注(0)|答案(2)|浏览(145)

我有两个按钮validate和notify,如下面的XML图像所示。

我想以编程方式在通知按钮下方显示验证按钮。如下图所示

我已经实现了Kotlin代码和代码snipped下面提到:

fun changeConstraint() {
    val valetBtnParent = binding.root.findViewById<ConstraintLayout>(R.id.childLayout)
    ConstraintSet().apply {
        val layoutParams = ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
        clone(valetBtnParent)
        clear(R.id.button_validate, ConstraintSet.END)
        clear(R.id.button_validate, ConstraintSet.BOTTOM)
        clear(R.id.button_validate, ConstraintSet.START)
        connect(R.id.button_validate, ConstraintSet.BOTTOM, R.id.detail_cardview, ConstraintSet.TOP, ZERO)
        connect(R.id.button_validate, ConstraintSet.END, R.id.detail_cardview, ConstraintSet.END, ZERO)
        connect(R.id.button_validate, ConstraintSet.START, R.id.button_notify, ConstraintSet.BOTTOM, TWO_HUNDRED)
        applyTo(childLayout)
    }
}

请救救我吧!

ttisahbt

ttisahbt1#

您忽略了一个非常重要的事情,那就是XML布局,这在ContraintLayout动画中非常重要,因为它告诉您现有的约束是什么,但让我们只处理提供的代码:
这似乎是预期结果:

要实现这一目标:您必须清除button_notifybutton_validateStart(以及Bottom,如果正在使用)的TopBottomEnd约束。
为此,您可以将代码更改为:

fun changeConstraint() {
    ConstraintSet().apply {
        clone(binding.childLayout)
        clear(binding.buttonValidate.id, ConstraintSet.TOP)
        clear(binding.buttonValidate.id, ConstraintSet.BOTTOM)
        clear(binding.buttonValidate.id, ConstraintSet.END)
        clear(binding.buttonNotify.id, ConstraintSet.START)
        connect(binding.buttonValidate.id, ConstraintSet.BOTTOM, binding.detailCardview.id, ConstraintSet.TOP)
        connect(binding.buttonValidate.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
        connect(binding.buttonValidate.id, ConstraintSet.TOP, binding.buttonNotify.id, ConstraintSet.BOTTOM, TWO_HUNDRED)
        connect(binding.buttonNotify.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        applyTo(binding.childLayout)
    }
}

假设detail_cardview在这些按钮下面,上面的代码应该可以正常工作。
要在更改约束时添加动画,请将以下行粘贴到包含applyTo()的最后一行上方:

val transition: Transition = ChangeBounds()
transition.interpolator = AccelerateInterpolator(1.0f)
transition.duration = 400
TransitionManager.beginDelayedTransition(binding.childLayout, transition)

如果需要添加动画监听器,您可以查看我对ConstraintSet动画here的其他回答。
因为你已经在使用视图绑定了,所以没有使用静态ID的逻辑,我也把它们改成了绑定ID。虽然你的代码不完整,但是应该可以工作。如果有什么地方遗漏了,一定要告诉我。
另外,要实现GIF中所看到的输出,我使用的GIF代码如下:

<!-- begin snippet: js hide: true -->
//To change the constraints to the new position
private fun changeConstraint() {
    ConstraintSet().apply {
        clone(binding.childLayout)
        clear(binding.buttonValidate.id, ConstraintSet.END)
        clear(binding.buttonNotify.id, ConstraintSet.START)
        clear(binding.buttonValidate.id, ConstraintSet.TOP)
        clear(binding.buttonValidate.id, ConstraintSet.BOTTOM)
        connect(
            binding.buttonValidate.id,
            ConstraintSet.END,
            ConstraintSet.PARENT_ID,
            ConstraintSet.END
        )
        connect(
            binding.buttonValidate.id,
            ConstraintSet.TOP,
            binding.buttonNotify.id,
            ConstraintSet.BOTTOM
        )
        connect(
            binding.buttonNotify.id,
            ConstraintSet.START,
            ConstraintSet.PARENT_ID,
            ConstraintSet.START
        )
        val transition: Transition = ChangeBounds()
        transition.interpolator = AccelerateInterpolator(1.0f)
        transition.duration = 400
        TransitionManager.beginDelayedTransition(binding.childLayout, transition)
        applyTo(binding.childLayout)
        isChanged = true
    }
}

//To change the constraints back to the original position
private fun changeConstraintBack() {
    ConstraintSet().apply {
        clone(binding.childLayout)
        clear(binding.buttonValidate.id, ConstraintSet.END)
        clear(binding.buttonNotify.id, ConstraintSet.START)
        clear(binding.buttonValidate.id, ConstraintSet.TOP)
        connect(
            binding.buttonValidate.id,
            ConstraintSet.END,
            binding.buttonNotify.id,
            ConstraintSet.START,
            5
        )
        connect(
            binding.buttonValidate.id,
            ConstraintSet.TOP,
            ConstraintSet.PARENT_ID,
            ConstraintSet.TOP
        )
        connect(
            binding.buttonValidate.id,
            ConstraintSet.BOTTOM,
            ConstraintSet.PARENT_ID,
            ConstraintSet.BOTTOM
        )
        connect(
            binding.buttonNotify.id,
            ConstraintSet.START,
            binding.buttonValidate.id,
            ConstraintSet.END
        )
        val transition: Transition = ChangeBounds()
        transition.interpolator = AccelerateInterpolator(1.0f)
        transition.duration = 400
        TransitionManager.beginDelayedTransition(binding.childLayout, transition)
        applyTo(binding.childLayout)
        isChanged = false
    }
}
omhiaaxx

omhiaaxx2#

在我看来,最简单的方法是将它们插入线性布局,并将方向从水平更改为垂直。

相关问题