Android:如何以编程方式设置layout_constraintRight_toRightOf“parent”?

67

我有一个ConstrainLayout中的视图,如下所示。

<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"


1
@W4R10CK 我不想添加视图,我只想改变对齐方式。你可以发布相关答案的部分吗? - Relm
5个回答

97

以下是使用Java代码将按钮设置到父视图底部的示例:

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代码应该像这样

set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);

并且要实现类似这样的效果,

layout_constraintLeft_toRightOf="@+id/parent"

您的Java代码应该如下所示:

set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);

在这里,我假设 android:id="@+id/parent" 是您的父级 ConstraintLayout 的ID。


1
set.applyTo(layout); 这会将约束应用于父视图而不是 TextView,应该改为 set.applyTo(button); 或类似的内容吗? - Relm
1
现在集合已经知道你想要应用哪些更改,现在是时候告诉父布局:“嘿,请应用这些更改了。” 因此,应该像 set.applyTo(parent) 这样设置。 - Darshan Soni
1
你的代码有误,首先你必须在约束布局中添加按钮,然后才能执行constraintSet.clone(constraintLayout);。否则,按钮将不会被处理。 - Khyati Vara

17

constraintSet = 新的 ConstraintSet(); 等等

无论如何,对我都没有用。

通过解决

  LayoutParams layoutParams = (LayoutParams) viewToChange.getLayoutParams();
    layoutParams.leftToLeft = anotherViewId;
    layoutParams.rightToRight =anotherViewId;
    layoutParams.topToTop = anotherViewId;
    layoutParams.bottomToBottom = anotherViewId;
    layoutParams.startToStart =anotherViewId;
    layoutParams.endToEnd = anotherViewId;
    viewToChange.setLayoutParams(layoutParams);

有人知道为什么在某些情况下约束集合就是不起作用吗?当使用LayoutParams时,它对我来说很好用。 - tamj0rd2
这个方法对我有用 - 我想改变LinearLayout的参数,它的父布局是ConstrainLayout。谢谢。 - Adarsh Vijayan P

6
使用id
class MyLayout(context: Context) : ConstraintLayout(context) {

    fun show() {
        val view = ImageView(context)
        addView(view)
        val params = view.layoutParams as ConstraintLayout.LayoutParams
        params.height = 100 
        params.width = 50
        params.rightToRight = id
        view.requestLayout()
    }
}

它从view.layoutParams返回null。 - gmartinsnull

3
我还没有看到比这更好的解决方案:
textMain.updateLayoutParams<ConstraintLayout.LayoutParams> {
    startToEnd = anyOtherView.id
    topToTop = anyOtherView.id
    endToStart = anyOtherView.id
    bottomToBottom = anyOtherView.id
  }

3

我在需要移动 buttons 以便新的 EditText 出现时也遇到了同样的情况,在 XML 文件中,它们有如下约束:

app:layout_constraintTop_toBottomOf="@+id/first_layout"

而我所需要做的是以编程的方式将其更改为诸如以下的内容:

app:layout_constraintTop_toBottomOf="@+id/second_layout"

使用 ConstraintLayout.LayoutParams 很容易完成该任务。例如:

Kotlin:

val currentLayout = btn.layoutParams as ConstraintLayout.LayoutParams // btn is a View here
currentLayout.topToBottom = R.id.second_layout // resource ID of new parent field
btn.layoutParams = currentLayout

就是这样了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接