android 以编程方式移除/编辑ConstraintLayout内视图的约束

a5g8bdjr  于 2023-02-02  发布在  Android
关注(0)|答案(2)|浏览(220)

我在ConstraintLayout中有一个视图(Linearlayout),它具有以下属性:

android:id="@+id/myView"
 app:layout_constraintTop_toBottomOf="@+id/anotherView"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

在某些情况下,我希望通过简单地删除右约束来将myView向左对齐,但我无法做到这一点。
我尝试使用以下代码,只是为了复制和应用约束参数:

ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.myView.getLayoutParams());
 holder.myView.setLayoutParams(layoutParams);

但是layoutParams总是接收一个空的参数集,并将视图发送到左上角,也许是因为我在recyclerView内部使用了这个函数?
recyclerView内部外观:

public void onBindViewHolder(final RecyclerView.ViewHolder basicHolder, int position) {
ProfileAdapter.UserInfoViewHolder holder = (ProfileAdapter.UserInfoViewHolder) basicHolder;
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.myView.getLayoutParams(‌​));
holder.myView.setLayoutParams(layoutParams); 
}
u0sqgete

u0sqgete1#

在设置布局参数时有一个bug(将在下一个版本中修复)。同时,您可以:

ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.myView.getLayoutParams();
layoutParams.rightToRight = ConstraintLayout.LayoutParams.UNSET;
holder.myView.setLayoutParams(layoutParams);
7gcisfzg

7gcisfzg2#

在Kotlin也是如此:

val layoutParams = view.layoutParams as ConstraintLayout.LayoutParams
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET

相关问题