0
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);
}

我尝试ImageView在坐标 10,100 处放置一个新的。我也尝试定位ImageViewa 2000,100 但ImageView无论哪种方式都始终出现在同一个地方。

我怀疑这与像素密度有关。浮点像素和 dpi 值之间有什么关系?

4

1 回答 1

1

setX()无论您遇到什么问题,它们都与orsetY()和像素密度无关。无论如何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()and setTranslationY()。如果您View的电话没有受到影响,setX()setY()会首先寻找其他原因。例如,您可能正在尝试调用错误setX(),或者稍后代码的另一部分可能会覆盖您的更改。我不能给你比你提供的信息更详细的答案。setY()View

于 2015-05-27T12:26:06.687 回答