笔记107--去除自定义AlertDialog黑边

1、现象描述

View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);
AlertDialog _ad = new AlertDialog.setView(_view).Builder(getActivity()).create();				
_ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
_ad.show();
效果图:

看到黑边了吧,真丑。

2、将就的解决方案

View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);
AlertDialog _ad = new AlertDialog.Builder(getActivity()).create();				
_ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
_ad.setView(_view, 0, 0, 0, 0);
_ad.show();
效果图:
虽然上下的黑边不见了,但是四周仍有个黑框。

3、更好的解决方案

通过样式文件把背景设置为透明。

View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);
AlertDialog _ad = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.Theme_Transparent)).create();				
_ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
_ad.setView(_view);
_ad.show();

样式任选其一即可:

<style name="Theme_Transparent" parent="@android:Theme.DeviceDefault.Light.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
</style>        
</style>
  <item name="android:windowFrame">@null</item>
</style>

效果图:


4、Perfect解决方案

上面的解决方案会导致AlertDialog无故变宽,而且如果你想加个圆角背景,会发现根本没效果~

其实你只需要了解一点:

setView()和setContentView()的区别:setView()只会覆盖AlertDialog的Title和Button之间的部分,而setContentView()则会全部覆盖。

注意:setContentView()必须在show()后面调用。

要改变AlertDialog的尺寸:只需调用ad.getWindow().setLayout(200, 250);此方法同样也需要在show()后调用,否则无效。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值