发现使用X布局,y坐标 [英] Find layout with x,y coordinate

查看:141
本文介绍了发现使用X布局,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我充满了为Android自定义按钮的应用程序。我想允许用户重新排列这些按钮的主页或应用程序面板的图像按钮。

我研究这个,发现我可以使用拖放放大器;拖放功能与使用者的运动进行交互。但在我的情况下,父母的布局可以是不同的。 OnMove或OnDrop事件,我需要真正的移动按钮,在相应的布局。

所以,问题是,我怎么能找到包含x坐标和放大器的布局; y和放按钮了。

  @覆盖
公共布尔onTouchEvent(MotionEvent事件){
    开关(event.getAction()){
    案例MotionEvent.ACTION_DOWN:
        状态= START_DRAGGING;
        打破;
    案例MotionEvent.ACTION_UP:
        状态= STOP_DRAGGING;
        打破;
    案例MotionEvent.ACTION_MOVE:
        如果(状态== START_DRAGGING){
            //parentLayout.setPadding((INT)event.getRawX(),0,0,0);
            // **这里做什么**
            parentLayout.invalidate();
        }
        打破;    }    返回true;
}


解决方案

您可以通过所有的控件在父容器循环,每个孩子的界限与当前的X相比,Y.,您可以通过调用该得到的意见范围:

<一个href=\"http://developer.android.com/reference/android/view/View.html#getHitRect%28android.graphics.Rect%29\">View.getHitRect()

因此​​,像这样:

 为(视图V:parent.children())
{
    //只检查ViewGroups(布局)很明显,你可以改变
    //这满足您的需求
    如果(!(五的instanceof的ViewGroup))
        继续;    如果(v.getHitRect()。含有(X,Y))
        返回伏;
}

这仅仅是Psuedo- code和将需要适应不管你用的是(即增加对嵌套控件递归)。

希望有所帮助。

I have an app filled with custom buttons for Android. I would like to allow user to rearrange these buttons like image buttons of Home or Application panel.

I researched on this and found out that I can use drag & drop functionality to interact with user's motion. But in my case parent layout can be different. OnMove or OnDrop event, I need to actually move that button in that corresponding layout.

So question is how I can find a layout that contains coordinate x & y and put the button in it.

@Override
public boolean onTouchEvent(MotionEvent event) {


    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        status = START_DRAGGING;
        break;
    case MotionEvent.ACTION_UP:
        status = STOP_DRAGGING;
        break;
    case MotionEvent.ACTION_MOVE:
        if(status == START_DRAGGING){
            //parentLayout.setPadding((int)event.getRawX(), 0,0,0);
            //**What to do here**
            parentLayout.invalidate();
        }                           
        break;

    }

    return true;
}

解决方案

You can loop through all the controls in the parent container and compare each child's bounds with the current X, Y. You can get a views bounds by calling this:

View.getHitRect()

So something like this:

for(View v : parent.children())
{
    // only checking ViewGroups (layout) obviously you can change
    // this to suit your needs
    if(!(v instanceof ViewGroup))
        continue;

    if(v.getHitRect().contains(x, y))
        return v;
}

This is just Psuedo-code and will need to be adapted for whatever you use is (i.e. adding recursion for nested controls).

Hope that helps.

这篇关于发现使用X布局,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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