如题,为什么在RelativeLayout中动态增加一个imageView,之前拖动过的控件又恢复了原来的位置,求大神解答,代码有点多,不过我都加注释了,求耐心看完,本人新手
配置文件
<RelativeLayout
        android:id="@+id/relative"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <FrameLayout
            android:background="#FFFFFF"
            android:layout_width="860dp"
            android:layout_height="498dp" android:layout_alignParentLeft="true" android:layout_marginLeft="197dp"
            android:layout_alignParentTop="true" android:layout_alignParentBottom="true">
    </FrameLayout>
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:id="@+id/image"
            android:src="@drawable/icon" android:layout_alignParentLeft="true" android:layout_marginLeft="62dp"
            android:layout_alignParentTop="true" android:layout_marginTop="166dp"/>
    <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Test"
            android:id="@+id/button"
            android:layout_alignLeft="@+id/image" android:layout_alignParentTop="true" android:layout_marginTop="61dp"
            android:onClick="testClick"/>
</RelativeLayout>
代码:
package com.example.testwidget.Activity;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.example.testwidget.R;/**
* Created with IntelliJ IDEA.
* User: yanxw
* Date: 13-3-11
* Time: 下午8:12
* To change this template use File | Settings | File Templates.
*/
public class TestWidget extends Activity {    String tag = "TestWidget";
    int screenWidth;
    int screenHeight;
    int lastX;
    int lastY;    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels - 50;        ImageView imageView=(ImageView)findViewById(R.id.image);
        //给imageView添加拖动监听
        imageView.setOnTouchListener(new WidgetTouch());
    }    /**
     * testButton的点击事件,就是在这个RelativeLayout布局里动态增加一个imageView
     * @param view
     */
    public void testClick(View view){
        RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative);
        int i =  relativeLayout.getChildCount();
        addWidget(88,125,48,48,R.drawable.test,i);
    }    /**
     *
     * @param left   距左边框的距离
     * @param top    距顶部的距离
     * @param width  控件的宽度
     * @param height 控件的高度
     * @param src    控件的资源ID
     * @param index  控件的下标
     */
    public void addWidget(int left,int top,int width,int height,int src,int index){
        ImageView test = new ImageView(this);
        test.setId(index);
        test.setVisibility(View.VISIBLE);
        Bitmap bm = BitmapFactory.decodeStream(getResources().openRawResource(src));
        test.setImageBitmap(bm);
        test.setClickable(true);
        test.setOnTouchListener(new WidgetTouch());
        RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = left;
        params.topMargin = top;
        params.alignWithParent = true;
        params.width = width;
        params.height = height;        /*
        为什么动态增加了一个imageView后,之前拖动过的控件又恢复到原来(拖动前)的位置了,
        有什么办法让拖动过的控件不动,还是在拖动后的位置么?
         */
        relativeLayout.addView(test, index, params);
    }    /**
     * 定义控件的拖动事件
     */
    class WidgetTouch implements View.OnTouchListener{
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int action=event.getAction();
            Log.i(tag, "Touch:"+action);
            switch(action){
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    Log.i(tag,"按下触摸,位置X:"+lastX+"  位置Y:"+lastY+
                            "控件位置 left:"+v.getLeft()+" top:"+v.getTop());
                    break;
                /**
                 * layout(l,t,r,b)
                 * l  Left position, relative to parent
                 t  Top position, relative to parent
                 r  Right position, relative to parent
                 b  Bottom position, relative to parent
                 * */
                case MotionEvent.ACTION_MOVE:
                    int dx =(int)event.getRawX() - lastX;
                    int dy =(int)event.getRawY() - lastY;                    int left = v.getLeft() + dx;
                    int top = v.getTop() + dy;
                    int right = v.getRight() + dx;
                    int bottom = v.getBottom() + dy;
                    if(left < 0){
                        left = 0;
                        right = left + v.getWidth();
                    }
                    if(right > screenWidth){
                        right = screenWidth;
                        left = right - v.getWidth();
                    }
                    if(top < 0){
                        top = 0;
                        bottom = top + v.getHeight();
                    }
                    if(bottom > screenHeight){
                        bottom = screenHeight;
                        top = bottom - v.getHeight();
                    }
                    v.layout(left, top, right, bottom);
                    Log.i(tag, "位置:" + left + ", " + top + ", " + right + ", " + bottom);
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
//                    if(left>=143){
//
//                    }
                    break;
            }
            return false;
        }
    }}
RelativeLayout动态增加控件位置