relativelayout运行时中的位置视图
我想向相对布局添加一个视图。 该视图必须添加到指定位置。
我有这个:
int x = ...;
int y = ...;
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
v.layout(x, y, x + v.getWidth(), y + v.getHeight());
return true;
}
});
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
ontouchlistener 效果很好..视图保持在我的手指上。 然而起始位置始终是 0, 0...我的 x 和 y 是什么并不重要。
有人有想法吗?
i want to add a view to a relativelayout.
this view must be added to a specified position.
i have this:
int x = ...;
int y = ...;
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
v.layout(x, y, x + v.getWidth(), y + v.getHeight());
return true;
}
});
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
the ontouchlistener works great.. the view stays with my finger.
however the startpostion is always 0, 0... it doesn't matter what my x and y is.
anyone a idee?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将按钮添加到父视图之前,您可能需要尝试设置按钮的位置。所以只需交换这两行:
就可以了:
You might want to try setting the position of your button before you add it to the parent view. So just swap these two lines:
to be this:
我有一个解决方法。
使用边距来定位浮动视图并不是很好。然而它对我有用。
i have a workaround.
it is not great to use margin for positioning floating views. however it works for me.