1.设置权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
2.必须写个类它里面有静态属性WindowMangaer 和 WindowManager.LayoutParams
public class MyManager {
private static WindowManager mWindowManager;
private static WindowManager.LayoutParams params;
public static void createView(Context context){
WindowManager windowManager = getWindowManager(context);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;//必须加上它不能会报错
params.gravity = Gravity.CENTER;
params.format = PixelFormat.RGBA_8888;//这最后加上它然显示会周围出现类似马赛克
params.width = 300;
params.height =200;
final View tempView = LayoutInflater.from(context).inflate(R.layout.my_view,null);
tempView.setLayoutParams(params);
tempView.findViewById(R.id.miss).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWindowManager.removeViewImmediate(tempView);
}
});
windowManager.addView(tempView,params);
}
private static WindowManager getWindowManager(Context context){
if(mWindowManager == null){
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
return mWindowManager;
}
3.在Activity中调用
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
2.必须写个类它里面有静态属性WindowMangaer 和 WindowManager.LayoutParams
public class MyManager {
private static WindowManager mWindowManager;
private static WindowManager.LayoutParams params;
public static void createView(Context context){
WindowManager windowManager = getWindowManager(context);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;//必须加上它不能会报错
params.gravity = Gravity.CENTER;
params.format = PixelFormat.RGBA_8888;//这最后加上它然显示会周围出现类似马赛克
params.width = 300;
params.height =200;
final View tempView = LayoutInflater.from(context).inflate(R.layout.my_view,null);
tempView.setLayoutParams(params);
tempView.findViewById(R.id.miss).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWindowManager.removeViewImmediate(tempView);
}
});
windowManager.addView(tempView,params);
}
private static WindowManager getWindowManager(Context context){
if(mWindowManager == null){
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
return mWindowManager;
}
3.在Activity中调用