自定义Dialog

public class DialogView extends Dialog {

    private boolean mIsCancelable;//控制点击dialog外部是否dismiss
    private View mView; //显示的Dialog 页面
    private int mGravity; //Dialog显示的屏幕位置
    private int mWidth; //Dialog显示的宽度
    private int mHeight;//Dialog显示的高度


    public DialogView(@NonNull Context context, View view) {
        super(context);
        this.mIsCancelable = true;
        this.mView = view;
        this.mGravity = Gravity.CENTER; //默认居中
        this.mWidth = WindowManager.LayoutParams.WRAP_CONTENT;
        this.mHeight = WindowManager.LayoutParams.WRAP_CONTENT;
    }

    public DialogView(@NonNull Context context, View view, boolean cancelable) {
        super(context);
        this.mIsCancelable = cancelable;
        this.mView = view;
        this.mGravity = Gravity.CENTER; //默认居中
        this.mWidth = WindowManager.LayoutParams.WRAP_CONTENT;
        this.mHeight = WindowManager.LayoutParams.WRAP_CONTENT;
    }


    public DialogView(@NonNull Context context, int themeResId, boolean cancelable, View view, int gravity, int width, int height) {
        super(context, themeResId);
        this.mIsCancelable = cancelable;
        this.mView = view;
        this.mGravity = gravity;
        this.mWidth = width;
        this.mHeight = height;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        setCancelable(mIsCancelable);
        setCanceledOnTouchOutside(mIsCancelable);
        Window window = this.getWindow();
        //设置透明背景
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setGravity(mGravity);

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = mWidth;
        params.height = mHeight;
        window.setAttributes(params);
    }
}

 

使用方法:

   View view = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null);
   DialogView dialog = new DialogView(this, view);
   // DialogView dialog = new DialogView(this,R.style.bottom_dialog,false,view,Gravity.BOTTOM,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
   dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            
        }
    });
   dialog.show();
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值