转载:
1: 加入权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2:
private WindowManager wm = null;
private WindowManager.LayoutParams wmParams = null;
private Button play1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化悬浮按钮
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
//启动的方法不用写在oncreate中
super.onResume();
play1 = new Button(this);
initFloatview();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//退出时删除
wm.removeView(play1);
}
private void initFloatview() {
wm = (WindowManager) getApplicationContext().getSystemService("window");
wmParams = new WindowManager.LayoutParams();
wmParams.type = LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
play1.getBackground().setAlpha(100);
play1.setText("播放");
wmParams.width = 210;
wmParams.height = 60;
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
wmParams.x = 30;
wmParams.y = 110;
wm.addView(play1, wmParams);
}