0

我需要在我的布局中放置多个新的 ImageView。问题是一个完全在同一位置出现在另一个之上。虽然我改变了位置,但它只与第一个有关。他们都在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);

}
4

2 回答 2

3

RelativeLayout有自己的Layout Parameters。要并排或垂直放置子视图,您需要提供规则。用于addRule()您添加的每个视图的布局参数。

将等值与要与之对齐的视图一起传递RelativeLayout.BELOWRelativeLayout.RIGHT_OFid

于 2012-12-25T17:35:24.023 回答
1

您的问题是您在第一次创建布局时设置了 layoutparams,然后它们不会像您想象的那样重新创建。

简单的解决方案。将其更改为下面的代码,该代码经我测试:

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

}
于 2012-12-25T17:45:54.453 回答