Removing/Editing 以编程方式约束 ConstraintLayout 内的视图
Removing/Editing a constraint of a view inside a ConstraintLayout programmatically
我在 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);
}
按照您的方式设置布局参数时存在错误(将在下一个版本中修复)。在此期间,您可以:
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.myView.getLayoutParams();
layoutParams.rightToRight = ConstraintLayout.LayoutParams.UNSET;
holder.myView.setLayoutParams(layoutParams);
我在 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);
}
按照您的方式设置布局参数时存在错误(将在下一个版本中修复)。在此期间,您可以:
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.myView.getLayoutParams();
layoutParams.rightToRight = ConstraintLayout.LayoutParams.UNSET;
holder.myView.setLayoutParams(layoutParams);