简体   繁体   English

Android无法将相对布局放置在正确的位置

[英]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. Android出于某种原因似乎将图像放置在屏幕的左上角。

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. 理想情况下,图像应放置在100,259处-但是,它似乎位于0,0处。 The "this" reference in my code is a Framelayout. 我的代码中的“ this”引用是一个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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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