动态按钮退出RelativeLayout

时间:2014-09-15 06:46:49

标签: android drag-and-drop relativelayout

我正在开发一个使用Drag and Drop功能的应用程序。 我正在生成按钮OnClick。代码工作正常,但当drag buttonrelativelayout的角落时,按钮离开了{{1} }}。我希望它留在layout内。 我正在分享我的代码和图像。从图像中你可以清楚地理解我的问题

enter image description here

提前致谢。

MainActivity.java

layout

activity_main.xml中

public class MainActivity extends Activity implements OnTouchListener {
    Button btnAddButton;
    RelativeLayout rl1;
    int i = 1;
    private int _xDelta;
    private int _yDelta;
    ViewGroup _root;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnAddButton = (Button) findViewById(R.id.btnAdd);

        rl1 = (RelativeLayout) findViewById(R.id.relative_layout);
        _root = (ViewGroup)findViewById(R.id.relative_layout);
        btnAddButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                add(v);

            }
        });
    }
    public void add(View v) {
        Button btn = new Button(MainActivity.this);
        //btn.setId(i);
        RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

        int a=(int) (Math.random()*100);
       // Toast.makeText(MainActivity.this, String.valueOf(Math.random()*100), 1).show();//double a=Math.random();
        layoutParam.leftMargin = 30+a;

        if (i > 1) {

            layoutParam.addRule(RelativeLayout.BELOW, (i - 1));
        }

        btn.setText("Button" + i);
        rl1.addView(btn, layoutParam);
        btn.setOnTouchListener(this);
        i++;
    } 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
         final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    _xDelta = X - lParams.leftMargin;
                    _yDelta = Y - lParams.topMargin;
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    v.setLayoutParams(layoutParams);
                    break;
            }
            _root.invalidate();
        return true;
    }
}

0 个答案:

没有答案