我在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);
}
答案 0 :(得分:4)
当你按照你的方式设置布局参数时,会出现一个错误(将在下一个版本中修复)。与此同时,你可以这样做:
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.myView.getLayoutParams();
layoutParams.rightToRight = ConstraintLayout.LayoutParams.UNSET;
holder.myView.setLayoutParams(layoutParams);