以编程方式创建约束布局链
Create constraint layout chain programmatically
我有一个约束布局,它由 2 个静态文本视图和一些以编程方式添加的按钮组成。我想创建一个包含所有按钮的链,但我无法使其正常工作,因为有几个按钮卡在布局的中间底部。
我将在下面提供代码,也许有人可以提示我做错了什么。
protected void onPostExecute(JSONArray result){
super.onPostExecute(result);
try {
ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.menu);
ConstraintSet set = new ConstraintSet();
List<Integer> chainIDs = new ArrayList<Integer>();
int medId;
for(int i=0; i < result.length() ; ++i)
{
if(i<1){
JSONObject json = result.getJSONObject(i);
Button btn = new Button(Menu.this);
btn.setId(i);
int startID = btn.getId();
btn.setText(json.getString("nume"));
btn.setBackgroundColor(getResources().getColor(R.color.yellow));
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
lp.setMargins(margins,margins,margins,margins);
cl.addView(btn, lp);
set.clone(cl);
set.connect(startID, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
set.connect(startID, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
Button btn1 = findViewById(startID);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
}
});
chainIDs.add(startID);
set.applyTo(cl);
Log.d("STATE",Integer.toString(i));
}else
{
JSONObject json = result.getJSONObject(i);
Button btn = new Button(Menu.this);
btn.setId(i);
medId = btn.getId();
btn.setText(json.getString("nume"));
btn.setBackgroundColor(getResources().getColor(R.color.yellow));
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
lp.setMargins(margins,margins,margins,margins);
cl.addView(btn, lp);
set.clone(cl);
set.connect(medId, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
set.connect(medId, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.applyTo(cl);
Button btn1 = findViewById(medId);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
}
});
chainIDs.add(medId);
Log.d("STATE",Integer.toString(i));
}
}
int[] chain = new int [chainIDs.size()];
Iterator<Integer> iterator = chainIDs.iterator();
for (int j = 0; j < chain.length; j++)
{
chain[j] = iterator.next().intValue();
}
set.createVerticalChain(R.id.descriere,ConstraintSet.BOTTOM,ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,chain,null, ConstraintSet.CHAIN_SPREAD);
set.applyTo(cl);
setContentView(cl);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
这里是实际的输出:https://imgur.com/a/sM0HBkT
所需的输出类似于:
开始布局
文本视图
按钮 1
按钮 2
.
.
.
按钮 n
布局结束
编辑
使用View.generateViewId()
方法后解决
零不是有效的视图 ID。请参阅 generateViewId() 了解如何生成有效 ID。
还要检查 createVerticalChain() 的参数是否正确。第二个参数应该是边而不是 id。
我有一个约束布局,它由 2 个静态文本视图和一些以编程方式添加的按钮组成。我想创建一个包含所有按钮的链,但我无法使其正常工作,因为有几个按钮卡在布局的中间底部。
我将在下面提供代码,也许有人可以提示我做错了什么。
protected void onPostExecute(JSONArray result){
super.onPostExecute(result);
try {
ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.menu);
ConstraintSet set = new ConstraintSet();
List<Integer> chainIDs = new ArrayList<Integer>();
int medId;
for(int i=0; i < result.length() ; ++i)
{
if(i<1){
JSONObject json = result.getJSONObject(i);
Button btn = new Button(Menu.this);
btn.setId(i);
int startID = btn.getId();
btn.setText(json.getString("nume"));
btn.setBackgroundColor(getResources().getColor(R.color.yellow));
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
lp.setMargins(margins,margins,margins,margins);
cl.addView(btn, lp);
set.clone(cl);
set.connect(startID, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
set.connect(startID, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
Button btn1 = findViewById(startID);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
}
});
chainIDs.add(startID);
set.applyTo(cl);
Log.d("STATE",Integer.toString(i));
}else
{
JSONObject json = result.getJSONObject(i);
Button btn = new Button(Menu.this);
btn.setId(i);
medId = btn.getId();
btn.setText(json.getString("nume"));
btn.setBackgroundColor(getResources().getColor(R.color.yellow));
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,153,getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,43,getResources().getDisplayMetrics());
int margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getDisplayMetrics());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(width,height);
lp.setMargins(margins,margins,margins,margins);
cl.addView(btn, lp);
set.clone(cl);
set.connect(medId, ConstraintSet.LEFT,ConstraintSet.PARENT_ID , ConstraintSet.LEFT, 0);
set.connect(medId, ConstraintSet.RIGHT,ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.applyTo(cl);
Button btn1 = findViewById(medId);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Menu.this,"Apasare button", Toast.LENGTH_LONG).show();
}
});
chainIDs.add(medId);
Log.d("STATE",Integer.toString(i));
}
}
int[] chain = new int [chainIDs.size()];
Iterator<Integer> iterator = chainIDs.iterator();
for (int j = 0; j < chain.length; j++)
{
chain[j] = iterator.next().intValue();
}
set.createVerticalChain(R.id.descriere,ConstraintSet.BOTTOM,ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,chain,null, ConstraintSet.CHAIN_SPREAD);
set.applyTo(cl);
setContentView(cl);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
这里是实际的输出:https://imgur.com/a/sM0HBkT
所需的输出类似于:
开始布局
文本视图
按钮 1
按钮 2
.
.
.
按钮 n
布局结束
编辑
使用View.generateViewId()
方法后解决
零不是有效的视图 ID。请参阅 generateViewId() 了解如何生成有效 ID。
还要检查 createVerticalChain() 的参数是否正确。第二个参数应该是边而不是 id。