简体   繁体   中英

Android cannot get relative layout to place in correct position

I cannot seem to position a relative layout based on the margins being set. Android seems to be placing the image at the top left corner of the screen for some reason.

ImageView iv = new ImageView(super.getContext());
iv.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin=100;
params.leftMargin=259;
this.addView(iv, params);    

Ideally the image should be placed at 100,259 - however, it appears to be positioned at 0,0. The "this" reference in my code is a Framelayout.

You can do this instead.

ImageView iv = new ImageView(super.getContext());
iv.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
this.addView(iv, params);
iv.setX(259);
iv.setY(100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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