设置悬浮按钮
1、清单文件添加权限
2、调用此方法 initFloatBtn()
WindowManagerwm;
WindowManager.LayoutParamswmlp;
LinearLayoutlinear;
ImageViewiv;
private void initFloatBtn() {
wm= (WindowManager) getSystemService(WINDOW_SERVICE);
wmlp=newWindowManager.LayoutParams();
wmlp.type= WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
wmlp.gravity= Gravity.BOTTOM| Gravity.RIGHT;
wmlp.format= PixelFormat.RGBA_8888;
wmlp.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
wmlp.x=10;
wmlp.y=80;
wmlp.width=80;
wmlp.height=80;
linear=newLinearLayout(getApplicationContext());
linear.setGravity(Gravity.BOTTOM| Gravity.LEFT);
//组件
iv=newImageView(this);//文本
iv.setImageResource(R.mipmap.add_photo);
linear.addView(iv);//将textview添加进布局
wm.addView(linear,wmlp);//窗口管理器添加这个部件
iv.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
ToastUtils.StrToast(mContext,"添加相册");
LogUtils.e(TAG,"WindowManagerWindowManagerWindowManager");
openChooser();
}
});
}
@Override
protected voidonStop() {
super.onPause();
LogUtils.e(TAG,"onStop()---onStop()");
if(wm!=null) {
wm.removeViewImmediate(linear);
linear=null;
}
}
@Override
protected voidonDestroy() {
super.onDestroy();
if(wm!=null) {
wm.removeViewImmediate(linear);
linear=null;
}
}