0

以下のコードでテキストビューの前に画像を動的に設定する必要があるため、これを行う方法.?私のコードは以下のとおりです..

if (attchStringArray.length > 0) {                      
    LinearLayout attachemntLayout = new   LinearLayout(mainContext);
    LinearLayout.LayoutParams layoutParam  = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    TextView attNameView = new TextView(mainContext);
    ImageView Ivbackground= new ImageView(mainContext);
    attNameView.setText(attchStringArray[2]);
    attachemntLayout.addView(attNameView,  layoutParam);            
    attachemntLayout.setBackgroundResource(R.drawable.bg_for_answers);
    attachmentsection.addView(attachemntLayout);
}
4

2 に答える 2

1

元のコードに 2 行追加しました。コメントで始まる行を確認してください。

編集:たとえば、いくつかの画像を使用して画像ビューをロードする必要があります。ImageView.setImageResource()またはImageView.setImageDrawable()またはImageView.setImageBitmap().

if (attchStringArray.length > 0) {

    LinearLayout attachemntLayout = new   LinearLayout(mainContext);

    // Changed width to WRAP_CONTENT
    LinearLayout.LayoutParams layoutParam  = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TextView attNameView = new TextView(mainContext);
    ImageView Ivbackground= new ImageView(mainContext);

    // You should put some content in the ImageView
    // ...

    attNameView.setText(attchStringArray[2]);

    // I am adding the image view before the text view
    attachemntLayout.addView(Ivbackground, layoutParam);

    attachemntLayout.addView(attNameView,  layoutParam);
    attachemntLayout.setBackgroundResource(R.drawable.bg_for_answers);

    attachmentsection.addView(attachemntLayout);
}
于 2012-08-30T09:45:58.460 に答える
0

あなたのためにこれを試しsetBackgroundDrawable()てくださいTextView

  yourTextView.setBackgroundDrawable(R.drwable.your_image_name);
于 2012-08-30T09:52:37.720 に答える