以编程方式删除/编辑ConstraintLayout中视图的约束

时间:2016-12-27 16:47:33

标签: android android-constraintlayout

我在Linearlayout内有一个视图(ConstraintLayout),其中包含以下属性:

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

1 个答案:

答案 0 :(得分:4)

当你按照你的方式设置布局参数时,会出现一个错误(将在下一个版本中修复)。与此同时,你可以这样做:

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