簡體   English   中英

以編程方式創建ImageButton時,不會將其添加到視圖中

[英]When creating an ImageButton programmatically it is not added to view

我想以編程方式將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);
}

任何建議將不勝感激。

請注意您的setContentView方法。 除此之外, 答案還可以助您一臂之力。

一切看起來都不錯,但是您永遠都不會為添加的ImageView定義資源ID。 結果,您的語句previewImg.getId()將返回“ NO_ID”

為新的ImageView設置一個id,如下所示:

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

這將添加圖像按鈕,但是您必須設置約束以適合您的需求。

而不是使用: 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);
}

需要設置 ImageButton的LayoutParams

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

previewImg.setLayoutParams(lp);

您可能還需要設置邊距和填充。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM