Android:如何以编程方式将layout_constraintRight_toRightOf设置为“父项”

yb3bgrhw  于 2023-01-28  发布在  Android
关注(0)|答案(6)|浏览(173)

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

polhcujo

polhcujo1#

下面是使用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。

vcudknz3

vcudknz32#

约束集=新约束集();等等
反正对我不起作用。
解决人

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);
nqwrtyyt

nqwrtyyt3#

使用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()
    }
}
cmssoen2

cmssoen24#

当我需要向下移动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

就是这样!

mv1qrgav

mv1qrgav5#

没有见过比这更好的解决方案:

textMain.updateLayoutParams<ConstraintLayout.LayoutParams> {
    startToEnd = anyOtherView.id
    topToTop = anyOtherView.id
    endToStart = anyOtherView.id
    bottomToBottom = anyOtherView.id
  }
bihw5rsg

bihw5rsg6#

在Android中,您可以使用ConstraintLayout.LayoutParams类以编程方式将layout_constraintRight_toRightOf属性设置为“parent”。

ConstraintLayout layout = findViewById(R.id.constraint_layout);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
view.setLayoutParams(params);

在上例中,“constraint_layout”是约束布局的ID,“view”是要对其设置约束的视图,“params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID”用于将layout_constraintRight_toRightOf属性设置为“parent”。
这会将视图的右侧设置为其父视图的右侧,使其与屏幕的右边缘对齐。您也可以使用ConstraintLayout.LayoutParams的setRightToRight(int rightToRight)方法。
还需要注意的是,在修改视图的布局参数之后,应该对视图调用requestLayout(),以便更改将反映在布局上。

相关问题