简体   繁体   English

如何在XML,Java中设置图片的坐标

[英]How can I set the coordinates of a picture in XML, Java

Hello I am trying to make an app with java and XML and I want to have a picture in a specific location on the page. 您好,我正在尝试使用Java和XML制作应用程序,并且希望在页面上的特定位置显示图片。 How would I do this? 我该怎么做?
I tried: 我试过了:

ImageView sprite = (ImageView)findViewById(R.id.sprite);
sprite.setX(0);
sprite.setY(500);

and I got an error at sprite.setX(0); 我在sprite.setX(0);出错了sprite.setX(0); and sprite.setY(500); sprite.setY(500);
setX or setY does not exist... setX或setY不存在...

First of all, I do not see any relationship with XML, you should post all your code so we can analyze it and find what that error produces It's strange that there are no setX and setY methods in your ImageView control, maybe you're trying to call setX and setY in the wrong place 首先,我看不到与XML的任何关系,您应该发布所有代码,以便我们可以对其进行分析并查找导致错误的原因奇怪的是,ImageView控件中没有setX和setY方法,也许您正在尝试在错误的地方调用setX和setY

But assuming no, you can try the following: 但是假设没有,您可以尝试以下操作:

AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(int width, int height, int x, int y)

sprite.setLayoutParams(param);

where 哪里

int X = 0;
int Y = 500;
YourLayout.LayoutParams param = new YourLayout.LayoutParams(sprite.getMeasuredWidth(), sprite.getMeasuredHeight(), X, Y);
sprite.setLayoutParams(param);

Also

Suppose you want a ImageView of size 80x90 on your screen at postion 90x60 假设您希望屏幕在位置90x60处具有大小为80x90的ImageView

RelativeLayout relativeLayout = new RelativeLayout(this);

ImageView sprite = (ImageView)findViewById(R.id.sprite);

//Where 80 is the width and 90 the height
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(80, 90);

// Setting position of our ImageView
//you can use the margins of the layout
layoutParams.leftMargin = 80;
layoutParams.topMargin = 90;
//layoutParams.rightMargin = 80; //Example
//layoutParams.bottomMargin = 90; //Example


relativeLayout.addView(imageView, layoutParams);

In any case, I recommend you post your complete code so we can help you 无论如何,我建议您发布完整的代码,以便我们为您提供帮助

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

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