Android View.getLayoutParams()leftMargin和topMargin给出0

时间:2016-04-29 13:05:36

标签: view ontouchevent android-relativelayout layoutparams

我在RelativeLayout中有一个View,其layout_alignParentBottom和layout_alignParentRight属性设置为true,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlroot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

<View
        android:id="@+id/v_green"
        android:layout_height="@dimen/square_size"
        android:layout_width="@dimen/square_size"
        android:background="@color/green"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

我希望能够移动这个视图。我试图实现如下:

@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;
                Log.d("MainActivity","Action_Down:X="+X+",Y="+Y+",xD="+xDelta+",yD="+yDelta+",lm="+lParams.leftMargin+",tm="+lParams.topMargin);             
                break;
            case MotionEvent.ACTION_UP:
                Log.d("MainActivity","Action_up");
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.d("MainActivity","Action_Pointer_Down");
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.d("MainActivity","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);

                Log.d("MainActivity","Action_Move:X="+X+",Y="+Y+",xD="+xDelta+",yD="+yDelta);
                break;
        }
        rootLayout.invalidate();
        return true;
    }

问题是找到layoutParams.leftMargin和layoutParams.topMargin为0(从日志中找到)所以当我触摸视图时它会跳到左上角然后我可以正确地移动它。为什么它在RelativeLayout的右下角时为0?事情是我在RelativeLayout的屏幕的四个角落有四个视图,我希望能够移动它们。都有同样的问题。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。问题是视图属性,如alignParentBottom或alignParentTop等于true。我通过从视图中删除这些属性来验证它,我可以拖动它们。所以我所做的是在onTouch的ACTION_DOWN内我检查了4个alignParentXXXX属性中是否有任何一个属于视图。如果发现设置为true,我将其设置为false并添加所需的初始边距。以下是2个属性的示例代码:

case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
            if(lParams.getRule(RelativeLayout.ALIGN_PARENT_BOTTOM)==RelativeLayout.TRUE)
            {
                lParams.topMargin = v.getTop();
                lParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
                Log.d("MainActivity","added Rule bottom");
            }
            if(lParams.getRule(RelativeLayout.ALIGN_PARENT_TOP)==RelativeLayout.TRUE)
            {
                lParams.bottomMargin = v.getBottom();
                lParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                Log.d("MainActivity","added Rule top");
            }

同样适用于其他2个属性。这对我来说很有用。我还在我的blog中写了完整的代码,以防将来有人遇到这个问题。