Android自定义的弹窗

放肆的年华 提交于 2020-02-19 03:49:56
package com.microduino.qoobot.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.microduino.qoobot.R;

/*
 * :Created by z on 2019/1/16
 */

public abstract class BaseDialog {
    
    private Dialog dialog;
    private Activity activity;
    
    public BaseDialog(Activity activity,int res){
        
        dialog = new Dialog(activity, R.style.BuildDialog);
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(res, null);
        initView(view);
        
        initWindow();
        
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
    }
    
    private void initWindow() {
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距

        //设置去除dialog中的系统状态栏
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // window.setWindowAnimations(R.style.dialog_anima);

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
    }
    
    public abstract void initView(View view);
    
    public void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            //dialog = null;
        }

    }
    
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!