dialog置底部显示

这个博客介绍了如何创建一个自定义的底部显示的Dialog基类——BaseDialog和DataCollectBaseDialog。BaseDialog扩展了Android的Dialog类,包含了ButterKnife注解绑定和Toast显示等基本功能。DataCollectBaseDialog进一步扩展了BaseDialog,用于底部弹窗,并设置了显示宽度、动画效果以及键盘回退关闭弹窗的逻辑。
摘要由CSDN通过智能技术生成

BaseDialog定义

public abstract class BaseDialog extends Dialog {

    protected Context context;
    protected Unbinder unbinder;
    protected View view;

    public BaseDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        bindView();
    }

    protected void bindView() {
        view = LayoutInflater.from(context).inflate(setContentView(), null);
        setContentView(view);
        unbinder = ButterKnife.bind(this, view);
    }

    protected abstract int setContentView();

    public void cancelOn() {
        unbinder.unbind();
        cancel();
    }

    protected void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
    protected void showToastLong(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
    protected void showToast(String msg,int length) {
        Toast.makeText(context, msg,length).show();
    }

    protected int getColor(int colorId){
        return context.getResources().getColor(colorId);
    }

}

定义底部显示dialog

public abstract class DataCollectBaseDialog extends BaseDialog {

    DataCollectCallBack callBack;

    public DataCollectBaseDialog(Context context, DataCollectCallBack callBack) {
        this(context, R.style.FullScreenDialogTheme);
        this.callBack = callBack;
    }

    public DataCollectBaseDialog(Context context, int themeId) {
        super(context, themeId);
    }

    @Override
    protected void bindView() {
        super.bindView();
        /** 设置弹窗显示宽度
        否则在设置ViewGroup.LayoutParams.MATCH_PARENT为LayoutWidth后,可能出现部分内容超出屏幕无法显示
        */
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        params.width = context.getResources().getDisplayMetrics().widthPixels;
        view.setLayoutParams(params);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        
        // 设置重心显示为底部
        getWindow().setGravity(Gravity.BOTTOM);
        // 设置从底部弹出的动画效果
        getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
        
       
        setOnKeyListener(((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && keyCode == KeyEvent.ACTION_DOWN) {
                cancelOn();
            }
            return false;
        }));
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值