我的应用程序在某些时候向用户显示一个按钮作为 TYPE_SYSTEM_ALERT,我试图让用户移动它。触摸和移动操作确实使用以下代码注册,但按钮不移动。我错过了什么?
//create button
mbtRec = new Button(ACR.getContext());
mbtRec.setText(R.string.start_recording);
mbtRec.setTypeface(null, Typeface.BOLD);
mbtRec.setBackgroundColor(Color.RED); //mbtRec.setBackgroundResource(R.drawable.ic_launcher);
mbtRec.setOnClickListener(recButtonOnClickListener);
mbtRec.setOnTouchListener(recButtonOnTouchListener);
//create layout params
recButtonLayoutParams = new WindowManager.LayoutParams();
recButtonLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED; //8
recButtonLayoutParams.format = PixelFormat.RGBA_8888; //PixelFormat.TRANSLUCENT
recButtonLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
recButtonLayoutParams.gravity = Gravity.TOP | Gravity.CENTER; //51
recButtonLayoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
recButtonLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
paramWindowManager.addView(this.mbtRec, recButtonLayoutParams);
OnTouchListener recButtonOnTouchListener = new View.OnTouchListener() {
@TargetApi(Build.VERSION_CODES.FROYO)
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
Tools.Log("Drag", "Started Dragging");
break;
case MotionEvent.ACTION_UP:
Tools.Log("Drag", "Stopped Dragging");
break;
case MotionEvent.ACTION_MOVE:
Tools.Log("Drag", "Dragging");
recButtonLayoutParams.x =(int) event.getRawX();
recButtonLayoutParams.y= (int) event.getRawY();
mbtRec.setLayoutParams(recButtonLayoutParams);
mbtRec.invalidate();
break;
default:
break;
}
return false;
}
};