Removing/Editing a constraint of a view inside a ConstraintLayout programmatically

*爱你&永不变心* 提交于 2019-12-11 03:16:58

问题


I have a view (Linearlayout) inside a ConstraintLayout with the following attributes:

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

At some occasions I wish to align myView to the left, by simply removing the right constraint. however I'm not able to do so.
I tried to use the following, simply to copy and apply the constraint parameters:

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

but the layoutParams always receives an empty parameters set,and sends the view to the top left corner. Perhaps it's because I'm using this inside of a recyclerView?

How it looks inside the 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:


There's a bug (will be fixed in the next release) when setting layout params the way you do it. In the meantime, you can do:

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


来源:https://stackoverflow.com/questions/41348957/removing-editing-a-constraint-of-a-view-inside-a-constraintlayout-programmatical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!