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 –

相關問題