2017-10-30 6 views
0

特定の制約レイアウトにプログラムでImageButtonを追加したいと思います。具体的なガイドラインにも制約があります。これまでのところ私は以下のメソッドを実装していますが、debugText以外のエラーは発生しませんが、何も起こりません。ImageButtonをプログラムで作成する場合、ビューには追加されません。

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); 
} 

アドバイスをいただければ幸いです。

答えて

0

これは、画像ボタンが追加されますが、あなたはあなたのニーズに合うように制約を設定するようになりました。 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); 
} 
+0

しかし、情報をありがとう、イメージや背景色はまだ表示されません。 –

+0

訂正!何かが現れています!それは適切な場所ではなく、以下のalexscmarの答えと組み合わせる必要がありましたが、私と一緒に働くには十分です。どうもありがとう! –

0

あなたのsetContentViewメソッドには注意してください。それ以外の答えはthisです。あなたの答えを得ることができます。

+0

アドバイスありがとうございますが、リンクされた質問はどちらもうまくいかないです。 –

0

すべては正常ですが、追加されたリソースIDは決して定義されません。ImageViewその結果、あなたの文previewImg.getId()は、次のように「NO_ID」

が新しいImageViewのIDを設定して返します。

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

あなたあなたのImageButtonのLayoutParamsを設定する必要があります。代わりに使用しての

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

previewImg.setLayoutParams(lp); 

もあなたは余白を設定する必要があるかもしれませんがとパディング

+0

ヘルプがありがとうございました!それは適切な場所ではなく、上記のvikas kumarの答えと組み合わせる必要があったが、私と一緒に働くには十分であった。どうもありがとう! –

関連する問題