去除自定义AlertDialog黑边
2016-05-11 19:27
260 查看
http://blog.csdn.net/mwj_88/article/details/45482421
1、现象描述
[html] view
plain copy
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、将就的解决方案
[html] view
plain copy
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、更好的解决方案
通过样式文件把背景设置为透明。
[html] view
plain copy
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();
样式任选其一即可:
[html] view
plain copy
<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>
[html] view
plain copy
</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()后调用,否则无效。
1、现象描述
[html] view
plain copy
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、将就的解决方案
[html] view
plain copy
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、更好的解决方案
通过样式文件把背景设置为透明。
[html] view
plain copy
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();
样式任选其一即可:
[html] view
plain copy
<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>
[html] view
plain copy
</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()后调用,否则无效。
相关文章推荐
- QT绘图应用:QT绘制一个盒图
- Spark-SparkSQL深入学习系列三(转自OopsOutOfMemory)
- hdu2897——邂逅明下(巴什博奕变形)
- Java 垃圾回收器学习
- C++——C/C++字符串处理大集合
- iOS UIKit:TableView之单元格配置(2)
- java七种反转字符串的方法
- STL map与Boost unordered_map
- Spark-SparkSQL深入学习系列二(转自OopsOutOfMemory)
- 模板类重载,模板类特化
- 利用R语言如何聚类
- POJ 2484-A Funny Game硬币排成环(博弈-找规律)
- C语言——数组指针和通过指针引用数组元素的方法总结
- oracle表回滚到一个指定时间的操作语句
- 34.数字在排序数组中出现的次数
- Fragment生命周期
- Android开发笔记之《Window下安装Ubuntu双系统,Grub无法显示Window选项》
- 购物车效果实现
- C++实验5数组选择
- xUtils简介