如何以编程方式修改约束布局?
how to MODIFY a constraint layout programmatically?
我得到如下视图:
<org.rayanmehr.atlas.shared.customview.CustomTextView
android:id="@+id/tvDownVoteCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintTop_toBottomOf="@+id/anchorHelper"
app:layout_constraintLeft_toRightOf="@+id/tvDownVoteIcon"
app:layout_constraintBottom_toBottomOf="@+id/imgComment"
/>
如何以编程方式修改 app:layout_constraintVertical_bias
或任何其他约束 属性 的值,而无需在我的 activity 中重新设置整个属性集?
免责声明:我没有使用过这个特定的功能。这只是我对如何尝试做的解释。
我认为你需要的是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
}
}
我刚刚在 here 中找到了答案,您可以使用 ConstraintSet
实现如下所示:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(context, R.id.activity_constraint);
//for example lets change the vertical bias of tvDownVoteIcon
float biasedValue = 0.2f;
constraintSet.setVerticalBias(R.id.tvDownVoteIcon, biasedValue);
//you can do any other modification and then apply
constraintSet.applyTo( (ConstraintLayout) findViewById(R.id.activity_constraint));
这是我所做的(不需要 ConstraintSet
,我们可以直接在约束本身上工作):
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) myView.getLayoutParams();
params.horizontalBias = 0.2f; // here is one modification for example. modify anything else you want :)
myView.setLayoutParams(params); // request the view to use the new modified params
当我在它下面有一个 SeekBar
和一个 TextView
(左+右对齐)时,它就像一个魅力,我想更新 TextView
位置在 SeekBar
的光标下,所以我必须更新 SeekBar
的 OnSeekBarChangeListener
的水平偏移参数
如果您使用的是 Kotlin 并且您有 androidx-core-ktx 库,您可以简单地执行以下操作:
someView.updateLayoutParams<ConstraintLayout.LayoutParams> { horizontalBias = 0.5f }
我得到如下视图:
<org.rayanmehr.atlas.shared.customview.CustomTextView
android:id="@+id/tvDownVoteCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintTop_toBottomOf="@+id/anchorHelper"
app:layout_constraintLeft_toRightOf="@+id/tvDownVoteIcon"
app:layout_constraintBottom_toBottomOf="@+id/imgComment"
/>
如何以编程方式修改 app:layout_constraintVertical_bias
或任何其他约束 属性 的值,而无需在我的 activity 中重新设置整个属性集?
免责声明:我没有使用过这个特定的功能。这只是我对如何尝试做的解释。
我认为你需要的是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
}
}
我刚刚在 here 中找到了答案,您可以使用 ConstraintSet
实现如下所示:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(context, R.id.activity_constraint);
//for example lets change the vertical bias of tvDownVoteIcon
float biasedValue = 0.2f;
constraintSet.setVerticalBias(R.id.tvDownVoteIcon, biasedValue);
//you can do any other modification and then apply
constraintSet.applyTo( (ConstraintLayout) findViewById(R.id.activity_constraint));
这是我所做的(不需要 ConstraintSet
,我们可以直接在约束本身上工作):
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) myView.getLayoutParams();
params.horizontalBias = 0.2f; // here is one modification for example. modify anything else you want :)
myView.setLayoutParams(params); // request the view to use the new modified params
当我在它下面有一个 SeekBar
和一个 TextView
(左+右对齐)时,它就像一个魅力,我想更新 TextView
位置在 SeekBar
的光标下,所以我必须更新 SeekBar
的 OnSeekBarChangeListener
如果您使用的是 Kotlin 并且您有 androidx-core-ktx 库,您可以简单地执行以下操作:
someView.updateLayoutParams<ConstraintLayout.LayoutParams> { horizontalBias = 0.5f }