ImageView image not appearing in ViewGroup
I have an ImageView
as a child of a ViewGroup
that is part of a GridView
. The drawable of the ImageView
does not appear, however I can see the background color, and if I use the drawable in setBackgroundDrawable
instead, it is displayed. I've tried changing every property and calling invalidate()
but none have made the drawable appear.
The Log.i
prints
android.graphics.drawable.BitmapDrawable@407bd4c0
setImageResource(int)
doesn't work either.
I'm using Honeycomb.
public MyViewGroup(Context context) {
super(context);
myImageView = new ImageView(context);
myImageView.setBackgroundColor(Color.RED);
myImageView.setScaleType(ImageView.ScaleType.CENTER);
Dra开发者_运维百科wable drawable = this.getResources().getDrawable(R.drawable.myimage);
myImageView.setImageDrawable(drawable);
Log.i("",drawable+"");
//myImageView.setImageResource(R.drawable.myimage);
addView(myImageView);
}
I omitted the onLayout code where I call setLeft()
setTop()
setRight()
and setBottom()
.
Any help is much appreciated. Thanks.
Update: Also doesn't work with android.R.drawable images. I'm stumped.
Maybe the problem is, that you don't set the layout parameters. Try:
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, WRAP_CONTENT);
myImageView .setLayoutParams(params);
Try setting getting the image as bitmap, then setting it as drawable.
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myimage)
Drawable drawable = new BitmapDrawable(bmp);"
myImageView.setImageDrawable(drawable);
Just out of curiosity: Why you do all of this in your constructor?
If the parent layout has width/height of fill_parent, changing it to wrap_content might do the trick.
I was having this problem in Honeycomb using an ImageView with a drawable in a layout - the image was there judging from the width of the dialog I had it appearing in, but I couldn't see the image itself. The ImageView's parent was a LinearLayout with fill_parent. Switching to wrap_content fixed it right up.
精彩评论