wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); final WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; params.type = WindowManager.LayoutParams.TYPE_PHONE; params.gravity = Gravity.LEFT+Gravity.TOP; params.setTitle("Toast"); SharedPreferences sharedPreferences = getSharedPreferences("xy", 0); int backX = sharedPreferences.getInt("x", 0); int backY = sharedPreferences.getInt("y", 0); //是以默认的gravity为原点的,默认的是center这里我们要重新设置gravity params.x = backX; params.y = backY; view = new TextView(this); view.setText(address); view.setTextSize(30f); view.setTextColor(Color.WHITE); view.setOnTouchListener(new OnTouchListener() { private int startX; private int startY; @Override public boolean onTouch(View arg0, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int endX = (int) event.getRawX(); int endY = (int) event.getRawY(); // 计算移动偏移量 int dx = endX - startX; int dy = endY - startY; // 更新浮窗位置 params.x += dx; params.y += dy; wm.updateViewLayout(view, params); startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; default: break; } return true; } });