how to MODIFY a constraint layout programmatically?

前端 未结 4 805
离开以前
离开以前 2021-02-18 13:56

Ive got a view like below :

 

        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-18 14:47

    Disclaimer: I haven't used this specific functionality. This is just my interpretation on how I would try to do it.

    I think that what you need is ConstraintSet. After obtaining it, you could modify it and apply it again. This is a relevant example from this article.

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        val constraintSet1 = ConstraintSet()
        constraintSet1.clone(constraintLayout)
        val constraintSet2 = ConstraintSet()
        constraintSet2.clone(constraintLayout)
        constraintSet2.centerVertically(R.id.image, 0)
    
        var changed = false
        findViewById(R.id.button).setOnClickListener {
            TransitionManager.beginDelayedTransition(constraintLayout)
            val constraint = if (changed) constraintSet1 else constraintSet2
            constraint.applyTo(constraintLayout)
            changed = !changed
        }
    }
    

提交回复
热议问题