3

我想基于某些条件以编程方式删除和添加约束条件。以下是截图:在ConstraintLayout中以编程方式删除/添加约束条件

The button "create ad" has a top constraint

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

here is button with removed constraint on top

所以在想同样的效果达到编程

这里是我试过的代码:

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 

编辑

tried usingConstraintSet还,但结果却是,即使不同不知我的RecyclerView(被设置为父视图的边界)正在消失

ConstraintSet set = new ConstraintSet(); 
    set.clone(parentView); 

    if (advertisements.size() > 0) { 

     recyclerView.setVisibility(View.VISIBLE); 
     txvMyAdEmptyText.setVisibility(View.GONE); 
     imvEmpty.setVisibility(View.GONE); 
     adapter.setList(advertisements); 
     adapter.notifyDataSetChanged(); 

    } else { 

     set.connect(btnCreateAd.getId(), ConstraintSet.TOP, imvEmpty.getId(), ConstraintSet.BOTTOM, 0); 

     recyclerView.setVisibility(View.GONE); 
     txvMyAdEmptyText.setVisibility(View.VISIBLE); 
     imvEmpty.setVisibility(View.VISIBLE); 
     adapter.setList(new ArrayList<Advertisement>()); 
    } 
    set.connect(btnCreateAd.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0); 
    set.connect(btnCreateAd.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0); 
    set.connect(btnCreateAd.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0); 

    set.applyTo(parentView); 
+0

看看这有助于https://stackoverflow.com/questions/5107740/how-do-i-programmatically-remove-an-existing-rule-that-was-defined-in-xml –

+0

@ValdioVeliu对不起,但这是为RelativeLayout PARAMS,我需要从constraintLayoutParams。 –

+0

你有没有打扰过谷歌,甚至看看API?这个答案不完全是你正在寻找,但应该给你一个想法https://stackoverflow.com/questions/41666566/constraintlayout-how-to-add-several-views-programmatically?rq=1 – Chisko

回答

9

我没有经历过你的代码工作,但下面说明如何使用ConstraintSet来打破和制定约束。

ConstraintSet set = new ConstraintSet(); 
    ConstraintLayout layout; 

    layout = (ConstraintLayout) findViewById(R.id.layout); 
    set.clone(layout); 
    // The following breaks the connection. 
    set.clear(R.id.bottomText, ConstraintSet.TOP); 
    // Comment out line above and uncomment line below to make the connection. 
    // set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0); 
    set.applyTo(layout); 
+0

非常感谢,它工作完美。 –

+0

终于有了一种以编程方式删除约束的方法,谢谢。 –

+0

恐怕你链接了错误的链接,@bmjohns –

相关问题