一、原理
WindowManager是直接取底层View来装在视图
二、用法
可以直接使用windowmanager,显示界面,即悬浮窗:
`private WindowManager mWindowManager;
private RelativeLayout mRelativeLayout;
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
mRelativeLayout = (RelativeLayout)LayoutInflater.from(mContext).inflate(R.layout.install_layout, null);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.install_layout, null);
`
上面创建了View以及WindowManager的对象,其实WindowManager加载的是,最终还是加载的Android里的View,最终通过addView方法把View放置窗体内
mWindowManager.addView(btnView, mParams);
这里涉及了显示的衡量参数,需要注意的是,此参数需要设置为以下,否则会报bad_token的错误:
mParams = new WindowManager.LayoutParams();
mParams.gravity = Gravity.TOP | Gravity.LEFT;
mParams.height = WindowManager.LayoutParams.MATCH_PARENT;
mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mParams.format = PixelFormat.RGBA_8888;
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;