public void onClick(View v) {
ImageView image = (ImageView) inflate.inflate(R.layout.ani_image_view, null);
mAllImageViews.add(image);
image.setX(10);
image.setY(100);
}
我尝试在坐标10,100处定位一个新的ImageView
。我还尝试将ImageView
定位为2000,100,但ImageView
始终出现在同一位置。
我怀疑这与像素密度有关。浮点像素和dpi值之间的关系是什么?
无论您有什么问题,它们都与setX()
或setY()
和像素密度无关。无论如何,setX()
和setY()
期望一定数量的像素。如果您查看setX()
和setY()
的源代码,您会看到:
/**
* Sets the visual x position of this view, in pixels. This is equivalent to setting the
* {@link #setTranslationX(float) translationX} property to be the difference between
* the x value passed in and the current {@link #getLeft() left} property.
*
* @param x The visual x position of this view, in pixels.
*/
public void setX(float x) {
setTranslationX(x - mLeft);
}
/**
* Sets the visual y position of this view, in pixels. This is equivalent to setting the
* {@link #setTranslationY(float) translationY} property to be the difference between
* the y value passed in and the current {@link #getTop() top} property.
*
* @param y The visual y position of this view, in pixels.
*/
public void setY(float y) {
setTranslationY(y - mTop);
}
换句话说,它们基本上只调用setTranslationX()
和setTranslationY()
。如果您的View
不受setX()
和setY()
调用的影响,我将首先寻找其他原因。例如,您可能试图在错误的View
上调用setX()
和setY()
,或者稍后您的代码的另一部分可能会覆盖您的更改。根据你提供的信息,我无法给你更详细的答复。