约束布局设置运行时 constraintStart_toEndOf

问题描述 投票:0回答:1

我有一个限制条件 app:layout_constraintStart_toStartOf="parent". 运行时我需要把这个约束条件改为 app:layout_constraintStart_toEndOf="@+id\myid" .

从我的研究中,我发现只有 constraintSet.connect(viewid, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 50); . 但如何实现我的要求与此。有什么想法吗?

android android-view android-constraintlayout android-design-library
1个回答
1
投票

这是在 kotlin 但这和 Java.

  1. 给一个身份证 rootLayout.
  2. 现在,使用这段代码。

    val constraintSet = ConstraintSet()
    constraintSet.clone(rootLayout) //Your rootLayout
    constraintSet.connect(
       yourView.id,                //ID of the view whose position you want to change
       ConstraintSet.START,      
       yourMyIdView.id,            //ID of the correspondent view
       ConstraintSet.END
    )
    constraintSet.applyTo(rootLayout) //Your rootLayout
    

小贴士:你也可以通过设置动态变化 animateChangetruerootLayout (XML),或者您可以使用 Transition 我一直比较喜欢的动画。

val transition: Transition = ChangeBounds()
transition.interpolator = AccelerateInterpolator() //Or Any other Interpolator
transition.duration = 700 //Duration
TransitionManager.beginDelayedTransition(rootLayout, transition) //Your rootLayout
constraintSet.applyTo(rootLayout) //Remember to put above 4 lines before this

你可以进一步添加 Alpha 的动画,使用 yourView.animate().alpha(1f).duration = 700 //1f (0 to 1) is the value and 700 is the duration.

记住,它和Java几乎没有什么不同,你只需要在其中添加上 ; 在最后可能。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.