Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

android - Adding custom View and ImageView to a single Layout

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...