//获取windowmanager 对象
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
//初始化桌面需显示的视图
View view = LayoutInflater.from(context).inflate(R.layout.message_item_im_left, null);
//获取layoutparams对象 设置view的在桌面显示的样式
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
//设置视图的宽高
params.width = WindowManager.LayoutParams.FILL_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//设置透明度
params.alpha = 80;
params.gravity=Gravity.LEFT|Gravity.TOP;
//以屏幕左上角为原点,设置x、y初始值
params.x = 0;
params.y = 0;
//把视图添加到桌面显示
wm.addView(view, params);
所需开启权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />