relativelayout运行时中的位置视图

Doo*_*mic 1 android relativelayout

我想为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());
Run Code Online (Sandbox Code Playgroud)

ontouchlistener工作得很好..视图留在我的手指.但是startpostion总是0,0 ...我的x和y是什么并不重要.

还有谁?

Doo*_*mic 6

我有一个解决方法.

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.leftMargin = x;
lp.topMargin = y;
relativelayout.addView(tmpTextButtonView, lp);
Run Code Online (Sandbox Code Playgroud)

使用margin来定位浮动视图并不好.但它对我有用.