Android:动态设置ImageView?

| 我正在尝试下载图像,并动态创建ImageView并将其添加到我的布局中,但不会显示该图像。这是我的代码:
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setImageDrawable(avatar);
theLayout.addView(image);
也许在添加ImageView之后需要刷新布局?您如何“刷新”?     
已邀请:
尝试以下代码,则无需刷新。将图片网址放入
inputurl
变量中。
InputStream is = null;
String inputurl = \" Enter url of ur image \";
try {
        URL url = new URL(inputurl);
        Object content = url.getContent();
        is = (InputStream) content;
        avatar = Drawable.createFromStream(is,\"src\");
} catch (MalformedURLException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setImageDrawable(avatar);
theLayout.addView(image);
    
我认为您必须为vp定义参数 如果u将定义参数,它将显示图像,如-
LinearLayout.LayoutParams Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        Params.setMargins(0,0,0,0);

        Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    
您可以使用另一个线程下载图像,下载完成后,您可以使用处理程序来更新可绘制对象。您可以在文档中了解如何创建另一个线程。这似乎并不容易,但是如果您想构建响应速度更快的android应用,最好学习如何做。 (标题)带有第二个线程的示例ProgressDialog http://developer.android.com/guide/topics/ui/dialogs.html     

要回复问题请先登录注册