将两个ImageViews编程 [英] Place two ImageViews programmatically

查看:127
本文介绍了将两个ImageViews编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将多个新ImageViews在我的布局。问题是,人能在另一个上面准确地在相同的位置。虽然我改变位置,只涉及第一个。他们都是在80,80。

  RelativeLayout.LayoutParams LP1 =新RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);    addpPicD(lp1,80,80);
    addpPicD(lp1,100,100);
}私人无效addpPicD(LP的LayoutParams,诠释兰,诠释经度)
{        lp.setMargins(兰,经度,0,0);
        ImageView的ImageView的=新ImageView的(本);
        imageView.setImageResource(R.drawable.dot_bl);
        imageView.setLayoutParams(LP);
        rel.addView(ImageView的);}


解决方案

您的问题是,您将在第一时间创建布局,然后,你会认为他们会是他们没有重新创建的LayoutParams。

简单的解决方案。它更改为code,低于该作品由我所测试:

  RelativeLayout.LayoutParams LP1 = NULL;addpPicD(lp1,80,80);
addpPicD(lp1,100,100);
}私人无效addpPicD(LP的LayoutParams,诠释兰,诠释经度)
{
    LP =新RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(兰,经度,0,0);
    ImageView的ImageView的=新ImageView的(本);
    imageView.setImageResource(R.drawable.dot_bl);
    imageView.setLayoutParams(LP);
    rel.addView(ImageView的);}

I need to place multiple new ImageViews in my layout. The problem is that one comes on top of another exactly at the same location. Although I change the location, it only relates to the first one. They both are at 80,80.

RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    addpPicD(lp1,80,80);
    addpPicD(lp1,100,100);
}

private void addpPicD(LayoutParams lp, int Lan, int Lon) 
{

        lp.setMargins(Lan, Lon, 0, 0);
        ImageView imageView = new ImageView(this);  
        imageView.setImageResource(R.drawable.dot_bl);  
        imageView.setLayoutParams(lp);
        rel.addView(imageView);

}

解决方案

Your problem is that you set the layoutparams the first time you create the layout and then they aren't recreated as you would think they would be.

Simple solution. Change it to the code below which works as tested by me:

RelativeLayout.LayoutParams lp1 = null;

addpPicD(lp1,80,80);
addpPicD(lp1,100,100);
}

private void addpPicD(LayoutParams lp, int Lan, int Lon) 
{
    lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(Lan, Lon, 0, 0);
    ImageView imageView = new ImageView(this);  
    imageView.setImageResource(R.drawable.dot_bl);  
    imageView.setLayoutParams(lp);
    rel.addView(imageView);

}

这篇关于将两个ImageViews编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆