我需要帮助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"
等等。
我知道我的回答很晚了,但我相信它会帮助到其他经常来这里的人。
这篇文章不是我写的,但我做了一些修改,也就是说,你应该努力在这里查看完整的文章
约束集
在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,设置水平和垂直偏差,水平和垂直中心,操作链和更多。
读得真不错。
再说一次,这只是一种适应。
To set constraints of image view to:
app:layout_constraintRight_toRightOf="@+id/check_answer1"
app:layout_constraintTop_toTopOf="@+id/check_answer1"
use:
ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0);
constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0);
constraintSet.applyTo(constraintLayout);
To set constraints of image view to:
app:layout_constraintRight_toRightOf="@+id/check_answer2"
app:layout_constraintTop_toTopOf="@+id/check_answer2"
use:
ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0);
constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0);
constraintSet.applyTo(constraintLayout);
我知道我的回答很晚了,但我相信它会帮助到其他经常来这里的人。
这篇文章不是我写的,但我做了一些修改,也就是说,你应该努力在这里查看完整的文章
约束集
在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,设置水平和垂直偏差,水平和垂直中心,操作链和更多。
读得真不错。
再说一次,这只是一种适应。
@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的更多信息和方法的链接-点击这里。
假设我们想要在运行时改变约束,使button1在点击时与button2对齐:
然后,有这样的布局:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintTop_toTopOf="@+id/button3"
app:layout_constraintBottom_toBottomOf="@+id/button3"
app:layout_constraintStart_toEndOf="@+id/button3"
android:layout_marginStart="0dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="0dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Button 2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.5" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Button 3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.223" />
</android.support.constraint.ConstraintLayout>
我们可以做以下几点:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button1.setOnClickListener {
val params = button1.layoutParams as ConstraintLayout.LayoutParams
params.leftToRight = button2.id
params.topToTop = button2.id
params.bottomToBottom = button2.id
button1.requestLayout()
}
}