【问题标题】:When creating an ImageButton programmatically it is not added to view以编程方式创建 ImageButton 时,它不会添加到视图中
【发布时间】: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


    【解决方案1】:

    这将添加图像按钮,但您必须设置约束以满足您的需求。

    而不是使用:previewImg.setImageBitmap(displayImage);

    使用这个:previewImg.setImageResource(R.mipmap.ic_launcher);

    public void addButtonImage () {
        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.setImageResource(R.mipmap.ic_launcher); // 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);
    }
    

    【讨论】:

    • 感谢您的信息,但是图像或背景颜色仍然没有出现。
    • 更正!现在出现了一些东西!它不在正确的位置,必须将它与下面的 alexscmar 答案结合起来,但足以让我使用。非常感谢!
    【解决方案2】:

    小心你的 setContentView 方法。除此之外,this 的答案可以让您得到答案。

    【讨论】:

    • 感谢您的建议,但链接的问题也不起作用。
    【解决方案3】:

    一切看起来都不错,但您从未为添加的ImageView 定义资源 ID。结果,您的语句previewImg.getId() 将返回“NO_ID”

    为新的ImageView 设置一个id,如下:

        previewImg.setId(View.generateViewId()); // API 17+
    

    【讨论】:

      【解决方案4】:

      需要设置 ImageButton 的 LayoutParams

      ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      
      previewImg.setLayoutParams(lp);
      

      您可能还需要设置边距和内边距。

      【讨论】:

      • 感谢您的帮助,现在出现了一些问题!它不在正确的位置,必须将其与上面 vikas kumar 的答案结合起来,但足以让我使用。非常感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多