android普通的弹窗使用比较多的还是Dialog和PopupWindow,开发系统相关的应用时遇到需要不依赖Activity的弹窗,参考其他的资料,在这里总结一下两种常用的方法
1、使用系统级提示框Dialog,这种方法比较简单,在alertdialog.show();之前设置dialog为系统级提示框
alertdialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
同时在AndroidManifest.xml文件中加入系统提示框权限
"android.permission.SYSTEM_ALERT_WINDOW">
2、使用WindowManager 的addView()方法弹窗,使用方法也不是很复杂
WindowManager wm = (WindowManager) getApplicationContext().getSystemService("window");
WindowManager.LayoutParams para = new WindowManager.LayoutParams();
//设置弹窗的宽高
para.height = LayoutParams.WRAP_CONTENT;
para.width = LayoutParams.WRAP_CONTENT;
//期望的位图格式。默认为不透明
para.format = 1;
//当FLAG_DIM_BEHIND设置后生效。该变量指示后面的窗口变暗的程度。
//1.0表示完全不透明,0.0表示没有变暗。
para.dimAmount = 0.6f;
para.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_DIM_BEHIND;
//设置为系统提示
para.type = LayoutParams.TYPE_SYSTEM_ALERT;
//获取要显示的View
final View mView = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.systempopup_layout, null);
//单击View是关闭弹窗
mView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(mView);
}
});
//显示弹窗
wm.addView(mView, para);
同时在AndroidManifest.xml文件中加入系统提示框权限
"android.permission.SYSTEM_ALERT_WINDOW">
其中para.flags以及LayoutParam的其他属性设置参考:
http://blog.sina.com.cn/s/blog_4b3c1f950100qd9s.html
3、使用第二种方法实现弹窗效
主界面MainAcitity
package com.zzw.systempopup;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnButton = (Button) findViewById(R.id.btn);
btnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final WindowManager wm = (WindowManager) getApplicationContext().getSystemService("window");
WindowManager.LayoutParams para = new WindowManager.LayoutParams();
//设置弹窗的宽高
para.height = LayoutParams.WRAP_CONTENT;
para.width = LayoutParams.WRAP_CONTENT;
//期望的位图格式。默认为不透明
para.format = 1;
//当FLAG_DIM_BEHIND设置后生效。该变量指示后面的窗口变暗的程度。
//1.0表示完全不透明,0.0表示没有变暗。
para.dimAmount = 0.6f;
para.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_DIM_BEHIND;
//设置为系统提示
para.type = LayoutParams.TYPE_SYSTEM_ALERT;
//获取要显示的View
final View mView = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.systempopup_layout, null);
//单击View是关闭弹窗
mView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(mView);
}
});
//显示弹窗
wm.addView(mView, para);
}
});
}
}
主界面布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开系统窗口"
/>
</RelativeLayout>
弹窗界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello world"
android:textSize="50sp"
android:textColor="#f00"
/>
</LinearLayout>
在AndroidManifest.xml文件添加权限
效果图
退出应用后效果图
单击弹窗关闭