弹窗

AlertDialog 

        AlertDialog.Builder builder = new AlertDialog.Builder(DeviceInfoActivity.this);
        builder.setTitle("重启");
        builder.setMessage("是否确认重启");
        builder.setPositiveButton("重启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // 重启
                present.sendOrder(cid, "97");
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.create().show();

BottomSheetDialog底部弹窗 

View view = LayoutInflater.from(this).inflate(R.layout.dialog_reboot, null);
BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
// 设置透明背景
dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
// show
dialog.show();

popupwindow

public class BasePopupWindow extends PopupWindow {

    private Window window;
    private boolean isTransparent = false;
    protected Context context;

    public BasePopupWindow(Context context) {
        super(context);
        this.context = context;
        if (context instanceof Activity) {
            this.window = ((Activity) context).getWindow();
        }
        popupwindowSetting();
    }

    private void popupwindowSetting() {
        setFocusable(true); // 设置PopupWindow可获得焦点
        setTouchable(true); // 设置PopupWindow可触摸
        setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 无背景
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    /**
     * 背景是否透明, 默认半透明
     *
     * @param isTransparent
     */
    public void setWindowTransparent(boolean isTransparent) {
        this.isTransparent = isTransparent;
    }

    // 设置背景透明度
    private void popOutShadow(PopupWindow popupWindow) {
        if (window == null && isTransparent) {
            return;
        }
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.7f;
        window.setAttributes(lp);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.alpha = 1f;
                window.setAttributes(lp);
            }
        });
    }
}
    

// ---------------------------------------------------------------------
private void showUpgradePopupWindow(View parentView) {
        // view
        View view = LayoutInflater.from(context).inflate(R.layout.view_camera, null);
        upgradePopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 点击外部消失
        upgradePopupWindow.setBackgroundDrawable(new BitmapDrawable());
        upgradePopupWindow.setOutsideTouchable(true);
        // bg
        backgroundAlpha(0.9f);
        upgradePopupWindow.setOnDismissListener(new popupDismissListener());
        // position
        upgradePopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

    /**
     * 添加新笔记时弹出的popWin关闭的事件,主要是为了将背景透明度改回来
     */
    public class popupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

 

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页