首先,获得相对布局。
RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
创建ImageView
ImageView iv=new ImageView(this);
创建LayoutParamer,可以线性布局,相对布局等。
RelativeLayout.LayoutParamer lp=new RelativeLayout.LayoutParamer(LayoutParamer.WRAP_CONTENT,LayoutParamer.WRAP_CONTENT);
lp.width=mWidth;
lp.height=mHeight;
lp.setMargins(0,0,0,0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);//相对于父类的
lp.addRule(RelativeLayout.RIGHT_OF,R.id.iv2);//相对于其他控件的,其他控件的id
rl.addView(iv,lp);
将布局中的控件删除
rl.removeView(iv);
添加控件时可以定义id,例如iv.setId(id);此处的id代表android:id="";
后边要获得此处id可以
iv=(ImageView)findViewById(id);
然后进行其他相关操作。