systemDialog设置以及无焦点设置

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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值