public class SystemDialog extends Dialog{
public static final int WIDTH_DEFAULT = WindowManager.LayoutParams.MATCH_PARENT;
public static final int HEIGHT_DEFAULT = WindowManager.LayoutParams.WRAP_CONTENT;
private int posX;
private int posY;
private int mWidth = WIDTH_DEFAULT;
private int mHeight = HEIGHT_DEFAULT;
private View rootView;
private SystemDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);
getWindow().setLayout(mWidth, mHeight);
}
public static SystemDialog createAndShowDialog(Context context, int style, View rootView, int width, int height) {
SystemDialog dialog = new SystemDialog(context, style);
dialog.getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
dialog.setWidth(width);
dialog.setHeight(height);
dialog.setRootView(rootView);
dialog.getWindow().setLayout(width, height);
dialog.show();
return dialog;
}
/**
*
* @param context
* @param style
* @param width
* @param height
* @return
*/
public static SystemDialog createDialog(Context context, int style, int width, int height) {
SystemDialog dialog = new SystemDialog(context, style);
// dialog.getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
// dialog.getWindow().setGravity(Gravity.CENTER);
// dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
dialog.setWidth(width);
dialog.setHeight(height);
dialog.getWindow().setLayout(width, height);
return dialog;
}
/**
*采用这个函数创建,style查看下面
*/
public static SystemDialog createDialog(Context context, int style) {
SystemDialog dialog = new SystemDialog(context, style);
//以下四句都不要少了
dialog.setDialogUnFocus(); //将Dialog设置获取焦点
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
dialog.getWindow().setWindowAnimations(-1);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
return dialog;
}
public SystemDialog setGravity(int gravity) {
getWindow().setGravity(gravity);
return this;
}
public SystemDialog setRootView(View rootView) {
this.rootView = rootView;
return this;
}
public SystemDialog setLayout(int width , int height){
mWidth = width;
mHeight = height;
return this;
}
public SystemDialog setDialogCancleble(boolean cancleble) {
this.setCancelable(cancleble);
return this;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (keyEventDispacher != null && keyEventDispacher.dispatchKeyEvent(event)) {
return true;
}
return super.dispatchKeyEvent(event);
}
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public View getRootView() {
return rootView;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
public int getWidth() {
return mWidth;
}
public void setWidth(int width) {
if (width <= 0) {
return;
}
this.mWidth = width;
}
public int getHeight() {
return mHeight;
}
public void setHeight(int height) {
if (height <= 0) {
return;
}
this.mHeight = height;
}
public void setKeyEventDispacher(KeyEventDispacher keyEventDispacher) {
this.keyEventDispacher = keyEventDispacher;
}
private KeyEventDispacher keyEventDispacher;
public interface KeyEventDispacher {
boolean dispatchKeyEvent(KeyEvent keyEvent);
}
//设置对话框为不可获取焦点
public void setDialogUnFocus(){
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ;
getWindow().setAttributes(lp);
}
}
R.style.statusdialog
<!--自定义Status 的Dialog的样式-->
<style name="statusdialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>