Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to add some images to the View of an app. The app uses a custom view for its normal working but I want some images along with the custom View. I have tried adding a Layout as the contenView in which I added the ImageView and the custom View, but its not comming to the screen. Can any one give me some idea on this... thanks in advance.

Code:

            MyView view;
            LinearLayout ly;
            LinearLayout.LayoutParams lp;
            LinearLayout.LayoutParams ImageViewlp;


           lp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

           ImageViewlp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

          ly=new LinearLayout(this);
          sketchBoard.setBackgroundColor(0);

          ImageView img=new ImageView(this);
          img.setBackgroundColor(R.drawable.img);
          ly.addView(img,ImageViewlp);

          view=new MyView(this);
          ly.addView(view);
          this.addContentView(ly, lp);

Edit:

   img.setBackgroundResource(R.drawable.img);

I got it solved by changing img.setBackgroundColor(R.drawable.img); with the above one

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are doing this, but this result in drawing a BG color of whatever value R.Drawable.img is

 img.setBackgroundColor(R.drawable.img);

should be

 img.setBackgroundResource(R.drawable.img);

or better yet actually use the imageview's drawable resource and do

 img.setImageResource(R.drawable.img);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...