Android 在ConstraintLayout中以编程方式删除/添加约束

Android 在ConstraintLayout中以编程方式删除/添加约束,android,android-layout,android-constraintlayout,android-layoutparams,Android,Android Layout,Android Constraintlayout,Android Layoutparams,我想根据某些条件以编程方式删除和添加约束。以下是截图: 我想像这样删除它,但在代码中: 因此,我们希望通过编程实现相同的效果 下面是我试过的代码: if (advertisements.size() > 0) { //my own condition ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();

我想根据某些条件以编程方式删除和添加约束。以下是截图:

我想像这样删除它,但在代码中:

因此,我们希望通过编程实现相同的效果

下面是我试过的代码:

    if (advertisements.size() > 0) { //my own condition
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view
        btnCreateAd.setLayoutParams(layoutParams);
        recyclerView.setVisibility(View.VISIBLE);
        txvMyAdEmptyText.setVisibility(View.GONE);
        imvEmpty.setVisibility(View.GONE);
        adapter.setList(advertisements);
        adapter.notifyDataSetChanged();
    } else {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work
        btnCreateAd.setLayoutParams(layoutParams);

        recyclerView.setVisibility(View.GONE);
        txvMyAdEmptyText.setVisibility(View.VISIBLE);
        imvEmpty.setVisibility(View.VISIBLE);
        adapter.setList(new ArrayList<Advertisement>());
    }
    mConstraintView.invalidate(); //this is my constraint view

我还没有看完您的代码,但下面说明了如何使用
ConstraintSet
来打破和创建约束

ConstraintSet set=newconstraintset();
约束布局;
布局=(ConstraintLayout)findViewById(R.id.layout);
克隆(布局);
//以下操作将断开连接。
set.clear(R.id.bottomText,ConstraintSet.TOP);
//注释掉上面的行,取消注释下面的行以建立连接。
//set.connect(R.id.bottomText,ConstraintSet.TOP,R.id.imageView,ConstraintSet.BOTTOM,0);
set.applyTo(布局);

另一种方法是使用ConstraintLayout.LayoutParams.UNSET。这种方式不使用约束集。清除

假设我们希望删除constraintLayout本身的底部约束,但可以是任何视图:

val containerParams = cl_container.layoutParams as ConstraintLayout.LayoutParams
containerParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
cl_container.layoutParams = containerParams

以下是来自using的答案的Java版本,它在不指定布局所约束的元素id的情况下工作:

final ConstraintLayout constraintLayout = findViewById(R.id.constraintLayout);
final ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) constraintLayout.getLayoutParams();
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET;
constraintLayout.setLayoutParams(layoutParams);

看看这是否有帮助@ValdioVeliu抱歉,但这是针对RelativeLayout参数的,我需要来自constraintLayoutParams的@Chisko我也尝试过ConstraintSet,但对这种情况不起作用,而且它打破了我对视图的其他约束。顺便说一句,所有视图都已在布局中创建(我的意思是我不会动态创建视图)。终于找到了一种以编程方式删除约束的方法,谢谢。。@DominikusK.Oops,谢谢你给我打电话!如果您对设置这些转换的动画感兴趣,这是一个很有帮助的方法。在更改约束时,是否有人注意到带有ConstraintLayout 1.1.1的Api 21或22存在任何问题?似乎有点不可靠,虽然在api 28Yes@Bakhshi上工作得很好,但我注意到了同样的事情。你解决了吗?如果是,如何进行?我正在使用版本1.1.3
set.clone(布局)这一行非常重要,因为我在没有这一行的情况下申请后,我的布局开始消失。请提供这一代码的java版本??Kotlin和java是可互换的。在ide或联机中查找更改它的选项
final ConstraintLayout constraintLayout = findViewById(R.id.constraintLayout);
final ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) constraintLayout.getLayoutParams();
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET;
constraintLayout.setLayoutParams(layoutParams);