Android:如何以编程方式将layout_constraintRight_toRightOf设置为“父" [英] 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设置为“父"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!