Android自定义Dialog没有按设置的宽度显示的问题

这篇博客主要探讨了在Android开发中遇到自定义Dialog未能按预期宽度显示的常见问题,并提供了解决方案。通过在Dialog的create()方法中添加特定代码,可以确保Dialog的宽度设置生效。
摘要由CSDN通过智能技术生成

针对自定义Dialog对话框(可以参考之前的博客)没有按设置的宽度显示的问题,可以采用如下方式解决:

Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
//将宽度值dp转px
lp.width = context.getResources().getDimensionPixelOffset(R.dimen.dp_210);
//高度自适应(也可设置为固定值,同宽度设置方法)
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);

即在之前自定义dialog的create()中添加以上代码即可.完整代码如下所示:

public ResultDisplayDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // instantiate the dialog with the custom Theme
            final ResultDisplayDialog dialog = new ResultDisplayDialog(context, R.style.Dialog);

            dialog.setCanceledOnTouchOutside(false);
            View layout = inflater.inflate(R.layout.layout_result_display_dialog, null);
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值