Android:如何以编程方式设置 layout_constraintRight_toRightOf “parent" [英] Android : How to programmatically set layout_constraintRight_toRightOf "parent"
问题描述
我在 ConstrainLayout 中有一个视图,如下所示.
I have a view in ConstrainLayout as follows.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="260dp"
android:textColor="#FFF"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="@+id/parent"
app:layout_constraintTop_toBottomOf="@id/message_date"
android:id="@+id/text_main"
/>
我想在 recycleViewHolder 中以编程方式将视图更改为 app:layout_constraintLeft_toLeftOf="@+id/parent"
或 layout_constraintLeft_toRightOf="@+id/parent"
基于某些条件.
I would like to change the view to either app:layout_constraintLeft_toLeftOf="@+id/parent"
or layout_constraintLeft_toRightOf="@+id/parent"
programmatically in recycleViewHolder based on some conditions.
推荐答案
下面是一个使用java代码在父视图底部设置按钮的例子:
Here is an example of setting a button to the bottom of parent view using java code:
ConstraintLayout constraintLayout;
ConstraintSet constraintSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout);
Button button = new Button(this);
button.setText("Hello");
constraintLayout.addView(button);
constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0);
constraintSet.constrainDefaultHeight(button.getId(), 200);
constraintSet.applyTo(constraintLayout);
}
要实现这样的目标,
app:layout_constraintLeft_toLeftOf="@+id/parent"
你的java代码应该是这样的
your java code should look like this
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
为了实现这样的目标,
layout_constraintLeft_toRightOf="@+id/parent"
你的java代码应该是这样的,
your java code should look like this,
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
这里,我假设 android:id="@+id/parent"
是你的父 ConstraintLayout 的 ID.
Here, I'm assuming android:id="@+id/parent"
is the id of your parent ConstraintLayout .
这篇关于Android:如何以编程方式设置 layout_constraintRight_toRightOf “parent"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!