[英]How to prevent double border when using packed chain style in ConstraintLayout
[英]Android ConstraintLayout - how to set margins of chain_packed elements programmatically
這是我的代碼:
ConstraintLayout.LayoutParams buttonParams = new ConstraintLayout.LayoutParams(40,105); buttonParams.setMargins(8,16,24,32); secretCodeFields = findViewById(R.id.secretCodeFields); for (int i = 0; i < 4; i++) { Button b = new Button(this); vID[i]=View.generateViewId(); b.setId(vID[i]); b.setLayoutParams(buttonParams); secretCodeFields.addView(b); } set.clone(secretCodeFields); set.createHorizontalChain( secretCodeFields.getId(),ConstraintSet.LEFT, secretCodeFields.getId(),ConstraintSet.RIGHT, vID,null,ConstraintSet.CHAIN_PACKED ); set.applyTo(secretCodeFields);
按鈕被添加在同一個地方(所以只有最后一個是可見的)。 如果我刪除“b.setLayoutParams(buttonParams);”,結果相同線和使用
secretCodeFields.addView(b,buttonParams);
但是,如果我添加帶有寬度和高度參數的按鈕,它可以工作,但沒有更多的邊距:
secretCodeFields.addView(b,90,90);
我做錯了什么?
編輯:secretCodeFields 是 ConstraintLayout
我找到了解決方案。 我在 set.applyTo... 之后添加了邊距:
for (int i = 0; i < codeLenght; i++) { Button b = (Button)findViewById(vID[i]); ConstraintLayout.LayoutParams p = (ConstraintLayout.LayoutParams) b.getLayoutParams(); p.setMargins(6,0,6,0); b.setLayoutParams(p); }
它有效,但我不知道它是否是最好的解決方案。
我知道這是一個遲到的答案,但在這里
ConstraintSet.CHAIN_PACKED
將刪除視圖的所有應用邊距,您應該首先在 Constraint Set 中創建鏈,然后使用 ConstrainSet connect(int startID, int startSide, int endID, int endSide, int margin)
方法應用邊距以指示邊距.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.