【发布时间】:2017-10-31 04:57:36
【问题描述】:
我想以编程方式将 ImageButton 添加到特定的约束布局。一种也受限于特定准则的方法。到目前为止,我已经实现了下面的方法,它没有给出任何错误,但是,除了 debugText 之外,似乎什么都没有发生。
public void addButtonImage () {
setContentView(R.layout.activity_homepage); // - Moved out of method
ConstraintLayout conL = (ConstraintLayout)findViewById(R.id.newLayout);
ImageButton previewImg = new ImageButton(this);
Guideline leftGl = (Guideline)findViewById(R.id.leftGideLine);
Guideline rightGL = (Guideline)findViewById(R.id.rightGuideLine);
ImageView header = (ImageView) findViewById(R.id.Header);
previewImg.setImageBitmap(displayImage); // displayImage variable assigned out of method
previewImg.setBackgroundColor(Color.parseColor("#FFFF00"));
conL.addView(previewImg);
ConstraintSet conS = new ConstraintSet();
conS.clone(conL);
conS.constrainHeight(pp.getId(), 90);
conS.constrainWidth(pp.getId(), 0);
conS.connect(previewImg.getId(), ConstraintSet.TOP, header.getId(), ConstraintSet.BOTTOM);
conS.connect(previewImg.getId(), ConstraintSet.LEFT, leftGl.getId(), ConstraintSet.RIGHT);
conS.connect(previewImg.getId(), ConstraintSet.RIGHT, rightGL.getId(), ConstraintSet.LEFT);
conS.applyTo(conL);
}
任何建议将不胜感激。
【问题讨论】:
标签: java android android-layout android-constraintlayout