简体   繁体   English

ImageView-如何以编程方式膨胀或添加?

[英]ImageView - how to inflate or add programmatically?

I am programmatically adding ImageView without success. 我以编程方式添加ImageView失败。 Is there any simple way to add ImageView into Activity programmatically? 有什么简单的方法可以通过编程将ImageView添加到Activity中吗?

Thx 谢谢

Edit: 编辑:

I have tried this and it is not working. 我已经尝试过了,但是它不起作用。

                for(Bitmap b: photoField){
                try{
                    Log.d("BIT", "Bitmapa "+b);
                    if(b!=null){
                        ImageView imv=new ImageView(PoiDisplay.this);
                        imv.setImageBitmap(b);
                    }
                }catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }

get the layout id in which u want to place the images then 获取您要在其中放置图像的布局ID,然后

Relative or Linear or any other layout..

RelativeLayout urLayoutId=(RelativeLayout)finViewById(R.id.RelativeLayoutIdInXml);

    for(Bitmap bitmap: photoField){
                    try{

                        if(bitmap!=null){
                            ImageView img=new ImageView(PoiDisplay.this);
                            img.setImageBitmap(bitmap);
                            urLayoutId.addView(img);
                        }
                    }catch (NullPointerException e) {
                        e.printStackTrace();
                    }
            }

Use this code 使用此代码

ImageView i=new ImageView(this);
i.setImageResource(R.drawable.ic_launcher);

you are only creating it not adding it. 您只是创建它而不添加它。 To add your view on layout. 在布局上添加视图。 First create or fetch layout. 首先创建或获取布局。

LinearLayout lp = (LinearLayout) findViewByid(R.id.linear_layout_name) LinearLayout lp =(LinearLayout)findViewByid(R.id.linear_layout_name)

            for(Bitmap b: photoField){
            try{
                Log.d("BIT", "Bitmapa "+b);
                if(b!=null){
                    ImageView imv=new ImageView(PoiDisplay.this);
                    imv.setImageBitmap(b);
                    lp.addview(imv)
                }
            }catch (NullPointerException e) {
                e.printStackTrace();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM