I have a two imageView in a Relative Layout. Is it

2019-08-02 09:22发布

问题:

I have Two Image Views Defined in Xml. I want to Change the Position of One of them Dynamically while the Other remains constant.?? Any Suggestions. Thanx.

回答1:

Yes off course, this is damn possible.Use LayouParam

        ImageView img1=(ImageView) findViewById(R.id.imageview);
    ImageView img2=(ImageView) findViewById(R.id.imageview);

    RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(100, 100);
    lp2.addRule(RelativeLayout.ALIGN_RIGHT,img2.getId());
    img1.setLayoutParams(lp2);

Setting Margin

lp2.setMargins(Left, TOp, right,Bottom);


回答2:

You can do it by providing new rules for RelativeLayout.LayoutParams. Just do something like this:

RelativeLayout.LayoutParams params = imageView2.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView2.setLayoutParams(params);


回答3:

Lets assume your imageView1 which would remain Constant is center aligned in parent. And Your Second ImageView imageView2 is aligned left to imageView1, and now you want to align it to below of imageView1. Use following to achieve the same,

RelativeLayout.LayoutParams params = imageView2.getLayoutParams(); param.addRule(RelativeLayout.BELOW, R.id.imageView1); imageView2.setLayoutParams(params);



回答4:

Use the layout property like this:

imageView.layout(l,t,r,b);

Where the l stands for left, t for top, etc.