拖拉时可拖拉按钮离开RelativeLayout [英] Draggable Button getting out of RelativeLayout whenever dragged

查看:200
本文介绍了拖拉时可拖拉按钮离开RelativeLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个我正在使用拖放功能的应用程序。
我正在生成按钮 OnClick 。代码正常工作,但是当我拖动 按钮 relativelayout 的角落,按钮从布局。我希望它保持在布局之内



拖动按钮之前 / p>



拖动按钮后(在此示例的上角)





正如你可以看到按钮布局中,每当我拖动 layout ,我希望它保持在布局中完整,显示完整的按钮。如何做到这一点?



提前感谢!



MainActivity.java

  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(菜单菜单){
getMenuInflater()。inflate(R.menu.main,menu);
返回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();
返回true;
}
}

activity_main.xml / p>

 < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / linear_layout
android:layout_width =fill_parent
android:layout_height =fill_parent>

< Button
android:id =@ + id / btnAdd
android:layout_width =wrap_content
android:layout_height =wrap_content
android:onClick =AddButton
android:text =Button/>

< RelativeLayout
android:id =@ + id / relative_layout
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentBottom =true
android:layout_alignParentRight =true
android:layout_alignParentTop =true
android:layout_toRightOf =@ + id / btnAdd>
< / RelativeLayout>

< / RelativeLayout>


解决方案

我能够通过使用以下条件解决这个问题:

  if((v.getY()< lay.getY())){

v.setX(XVAL);
v.setY(yVal);
} else if(v.getX()< lay.getX()){

v.setX(xVal);
v.setY(yVal);
} else if(v.getX()+ v.getWidth()> lay.getX()+ lay.getWidth()){

v.setX(xVal);
v.setY(yVal);
} else if(v.getY()+ v.getHeight()> lay.getY()+ lay.getHeight()){

v.setX(xVal);
v.setY(yVal);
} else {
for(int i = 0; i< = viewIdList.size() - 1; i ++){
if(v.getId()!= viewIdList.get i).getId()){

查看v3 = viewIdList.get(i);
Rect rect1 = new Rect(v.getLeft(),v.getTop(),
v.getRight(),v.getBottom());
v.getHitRect(rect1);
Rect rect2 = new Rect(v3.getLeft(),v3.getTop(),
v3.getRight(),v3.getBottom());
v3.getHitRect(rect2);
if(Rect.intersects(rect1,rect2)){

System.out.println(overlap);
v.setX(xVal);
v.setY(yVal);
}

其中v是视图(在我的情况下为ImageView),此代码已写入在onTouchListener的 MotionEvent.ACTION_UP 中。


I am developing an application in which I am using Drag and Drop functionality. In that I am generating buttons OnClick.The code is working fine but when I drag the button to the corners of the relativelayout,button gets out of the layout. I want it to stay inside of the layout.

Before Dragging the button

After Dragging the button (on top corner in this example)

As you can see that the button is getting out of the layout whenever I drag it towards the end/corner of the layout, I want it to stay in the layout intact, display full button. How can I do that?

Thanks in advance.!

MainActivity.java

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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="AddButton"
        android:text="Button" />

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnAdd" >
    </RelativeLayout>

</RelativeLayout>

解决方案

I was able to tackle this by using these conditions:

        if ((v.getY() < lay.getY())) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getX() < lay.getX()) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getX() + v.getWidth() > lay.getX() + lay.getWidth()) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getY() + v.getHeight() > lay.getY() + lay.getHeight()) {

            v.setX(xVal);
            v.setY(yVal);
        } else {
            for (int i = 0; i <= viewIdList.size() - 1; i++) {
                if (v.getId() != viewIdList.get(i).getId()) {

                    View v3 = viewIdList.get(i);
                    Rect rect1 = new Rect(v.getLeft(), v.getTop(),
                            v.getRight(), v.getBottom());
                    v.getHitRect(rect1);
                    Rect rect2 = new Rect(v3.getLeft(), v3.getTop(),
                            v3.getRight(), v3.getBottom());
                    v3.getHitRect(rect2);
                    if (Rect.intersects(rect1, rect2)) {

                        System.out.println("overlap");
                        v.setX(xVal);
                        v.setY(yVal);
                    }

where v is the view (ImageView in my case) and this code is written in MotionEvent.ACTION_UP of onTouchListener.

这篇关于拖拉时可拖拉按钮离开RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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