自定义AlertDialog,更改宽度,增加圆角

废话不多说,直接上代码:

 private void showLoginOutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(ApplicationAgent.getAgent().getContext(), R.layout.kq_dialog_login_out_layout, null);
        dialog.setCancelable(false);
        dialog.setView(view);

        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        TextView cancel = (TextView) view.findViewById(R.id.cancel_btn);
        TextView confirm = (TextView) view.findViewById(R.id.confirm_btn);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                loginOutSystem();
            }
        });
        if (!dialog.isShowing())
            dialog.show();

        WindowManager.LayoutParams lp = window.getAttributes();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = ScreenUtil.getScreenHeight(getApplicationContext()) / 10 * 8;
        } else {
            lp.width = ScreenUtil.getScreenWidth(getApplicationContext()) / 10 * 8;
        }
       window.setAttributes(lp);
    }

ScreenUtil工具类:

/**
     * 获取屏幕高度(px)
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

kq_dialog_login_out_layout.xml文件




    

    

    

    

        

就酱紫,惊不惊喜,意不意外?点个赞吧!_

你可能感兴趣的:(自定义AlertDialog,更改宽度,增加圆角)