去除自定义AlertDialog黑边

来源:互联网 发布:两张表格怎么比对数据 编辑:程序博客网 时间:2024/05/01 20:50
http://blog.csdn.net/mwj_88/article/details/45482421
1、现象描述

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);  
  2. AlertDialog _ad = new AlertDialog.setView(_view).Builder(getActivity()).create();                 
  3. _ad.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4. _ad.show();  
效果图:

看到黑边了吧,真丑。

2、将就的解决方案

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);  
  2. AlertDialog _ad = new AlertDialog.Builder(getActivity()).create();                
  3. _ad.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4. _ad.setView(_view, 0, 0, 0, 0);  
  5. _ad.show();  
效果图:
虽然上下的黑边不见了,但是四周仍有个黑框。

3、更好的解决方案

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. View _view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_schoolcourse, null);  
  2. AlertDialog _ad = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.Theme_Transparent)).create();                
  3. _ad.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4. _ad.setView(_view);  
  5. _ad.show();  

样式任选其一即可:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <style name="Theme_Transparent" parent="@android:Theme.DeviceDefault.Light.Dialog">  
  2.         <item name="android:windowIsTranslucent">true</item>  
  3.         <item name="android:windowBackground">@android:color/transparent</item>  
  4.         <item name="android:windowContentOverlay">@null</item>  
  5.         <item name="android:windowNoTitle">true</item>  
  6. </style>          
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. </style>  
  2.   <item name="android:windowFrame">@null</item>  
  3. </style>  

效果图:


4、Perfect解决方案

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

其实你只需要了解一点:

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

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

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

0 0