AlertDialog自定义内容区时宽高问题

问题描述:
创建AlertDialog,并使用setView()方法使用自定义的layout填充。虽然layout根容器宽高都设置为“match_parent”,但是最终显示的宽高都非常小。

原因分析:
AlertDialog默认会把根容器的宽高认为是wrap_content。
解决方法:
1.明确定义出根容器下级容器的宽高;
2.根容器加上任意值的minWidth和minHeight后,其layout_width和layout_height属性即可发挥作用。
3.使用对AlertDialog的WindowManager设置LayoutParams(需要在dialog.show()之后设置);
如:
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);   

备注:
使用Dialog类,并使用setContentView()方法设置时,宽度两边无边距。
使用AlertDialog时宽度两边有边距。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值