Android的setX的()和塞蒂()行为怪异 [英] Android setX() and setY() behaving weird

查看:1840
本文介绍了Android的setX的()和塞蒂()行为怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态地创建,然后在Android的活动而移动的图像。然而,setX的()和塞蒂()方法似乎无法正常工作。它正确地设置了一个图象的位置被首次创建时及放置,但任何试图更新它导致在图像中被放置在错误的位置。例如,在图像上下面code移动

I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly. It correctly sets the position of an image when it is first created and placed, but any attempt to update it results in the image being placed in the wrong spot. For instance, the image moves on the following code:

ImageView image;
RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_this);

if(action == MotionEvent.ACTION_DOWN){

    image = new ImageView(MyClass.this);                            
    layout.addView(image, width, height);   
    image.setX(206);
    image.setY(206);
}
else if(action == MotionEvent.ACTION_MOVE){

    if(image != null){
        image.setX(206);
        image.setY(206);
    }
}

在ACTION_MOVE图像移动,即使在x和y位置值保持不变。图像的父保持相同。大小保持不变。如果我得到的x和y的值还是会说206,但它不是在(20​​6,206)上的活动放在了。我很失落,为什么发生这种情况。我无法找到该图像已被改变,除了它的物理位置变化的迹象。

On ACTION_MOVE the image is moved even though the x and y position values remain the same. The parent of the image remains the same. The size remains the same. If I get the x and y values it will still say 206, but it is not placed at (206, 206) on the activity anymore. I am lost as to why this is happening. I can't find any indication that the image has been altered except for it physically changing location.

推荐答案

真的,这不应该发生。或者,尝试设置另一个变量和设置x和y它,或获取x和得到y和添加一个0到他们每个人对同一个位置。

Really, this shouldn't be happening. Alternatively, try setting another variable and setting x and y to it, or get x and get y and add a 0 to each one of them for same location.

作为<一说href=\"http://stackoverflow.com/questions/12195768/android-use-of-view-setx-and-sety-in-api-8\">Android - 使用view.setX的()和API 8 SETY,如果您搜索,还有另一种解决方案,即使API 8日前也适用。
的LayoutParams 是这样的 -

As stated in Android - Use of view.setX() and setY in api 8, if you have searched, there is another solution that also works even before api 8. LayoutParams works like this -

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);

有更多的信息出现。我希望这有助于

There is more information there. I hope this helps

这篇关于Android的setX的()和塞蒂()行为怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆