// 定义浮动窗口布局
private View mFloatLayout;
// 定义浮动窗口布局对象
private WindowManager.LayoutParams wmParams;
// 创建浮动窗口设置布局参数的对象
private WindowManager mWindowManager;
@SuppressWarnings("static-access")
private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
// 获取的是WindowManagerImpl.CompatModeWrapper
mWindowManager = (WindowManager) getApplication().getSystemService(
getApplication().WINDOW_SERVICE);
// 设置window type
wmParams.type = android.view.WindowManager.LayoutParams.TYPE_PHONE;
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.flags = android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.RIGHT | Gravity.TOP;
// 以屏幕左上角为原点,设置x、y初始值,相对于gravity
wmParams.x = 0;
wmParams.y = (int) getResources().getDimension(R.dimen.kaka_70_dip);
// 设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
/*
* // 设置悬浮窗口长宽数据 wmParams.width = 200; wmParams.height = 80;
*/
LayoutInflater inflater = LayoutInflater.from(getApplication());
// 获取浮动窗口视图所在布局
mFloatLayout = (View) inflater.inflate(R.layout.po_view, null);
// 添加mFloatLayout
mWindowManager.addView(mFloatLayout, wmParams);
// 浮动窗口按钮
// mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
createFloatView();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mWindowManager.removeView(mFloatLayout);
}