其他分享
首页 > 其他分享> > android AlertDialog 占满屏幕宽度

android AlertDialog 占满屏幕宽度

作者:互联网

网上找了很多设置弹框风格的文章,但是都不能设置成我想要的效果。最后参考了他们的一些处理方式,最后解决了,顺便记录一下处理。

初始的效果是这样的,下图:

 

 

    /**
     * 所有自定义创建和显示弹窗都不可以缺这方法
     */
    private void createAndShowDialog() {
        //创建弹窗
        View inputView = 
  LayoutInflater.from(getContext()).inflate(R.layout.main_dialog_editstreetandalley, null);
        AlertDialog mDialog = new AlertDialog.Builder(getContext())
                .setCancelable(true)
                .setView(inputView)
                .create();

        //位置居中
        Window window = mDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
   
        //设置
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
        window.getDecorView().setBackgroundColor(Color.WHITE);
        try {
            mDialog.show();
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

    }

最后得到效果:

 

 

标签:mDialog,layoutParams,LayoutParams,getContext,AlertDialog,window,android,屏幕
来源: https://blog.csdn.net/qq_36771930/article/details/122713777