Android原生AlertDialog使用总结
来源:互联网 发布:mac不能登陆两个qq 编辑:程序博客网 时间:2024/05/21 19:44
前言
在android.support.v7.app.AlertDialog未出来之前,安卓开发者基本上都是使用自定义的View来实现弹框。而如今,android.support.v7.app.AlertDialog的弹框样式与效果以及能支持的扩展已经能满足我们了,所以,越来越多的人也开始重新来使用Android提供的这个弹框控件了。但是有时候我们也还是需要来自定义一些属性,此文就是博主使用过程中遇到的一些问题以及解决办法。
原生基础使用
// import android.support.v7.app.AlertDialog;new AlertDialog.Builder(this) .setTitle("标题") .setMessage("内容") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create().show();
自定义标题、内容、按钮控件的大小与字体颜色
实际使用中,为了界面更美观、弹框与app整体效果更融洽,我们需要自定义标题、内容、按钮控件的大小与字体颜色等,而我们并未在android.support.v7.app.AlertDialog下找到显眼的设置的方法。假设我们需要实现这样的界面,则需要在dialog.show()
之后来分别对应设置:
AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("标题") .setMessage("内容") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create();dialog.show();// 在dialog执行show之后才能来设置TextView tvMsg = (TextView) dialog.findViewById(android.R.id.message);tvMsg.setTextSize(16);tvMsg.setTextColor(Color.parseColor("#4E4E4E"));dialog.getButton(dialog.BUTTON_NEGATIVE).setTextSize(16);dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#8C8C8C"));dialog.getButton(dialog.BUTTON_POSITIVE).setTextSize(16);dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#1DA6DD"));try { Field mAlert = AlertDialog.class.getDeclaredField("mAlert"); mAlert.setAccessible(true); Object alertController = mAlert.get(dialog); Field mTitleView = alertController.getClass().getDeclaredField("mTitleView"); mTitleView.setAccessible(true); TextView tvTitle = (TextView) mTitleView.get(alertController); if (null != tvTitle) { tvTitle.setTextSize(16); tvTitle.setTextColor(Color.parseColor("#000000")); }} catch (Exception e) { e.printStackTrace();}
项目中为了统一所有弹框演示,使用了封装工具类,在show之后,设置好相应的属性后,执行了dismiss方法返回dialog,在外部再调用dialog的show方法。测试发现,部分手机上对话框会无法显示出来,仅显示半透明背景层。具体原因未知。后改为工具类中直接show,后续不再依次执行dismiss、show方法。
0 0
- Android原生AlertDialog使用总结
- android 原生Sqlite使用总结
- Android开发入门之AlertDialog使用总结
- Android AlertDialog总结
- AlertDialog 使用总结
- AlertDialog总结及其使用
- android alertDialog的使用
- android基础AlertDialog使用
- Android AlertDialog使用
- android alertDialog 使用
- Android系统AlertDialog使用
- Android中AlertDialog使用
- android之使用AlertDialog
- Android Alertdialog使用案例
- Android AlertDialog使用
- 原生VideoView使用总结
- MaterialDialog 仿Android 5.0原生的AlertDialog样式的对话框
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- github中的pull request
- 弹出popwindow 背景变暗
- Bootstrap 学习之(八)------ 按钮组
- 【POJ3694】Network
- // 编写一个程序,在E盘下创建一个abc.txt的文本文档,通过输出流在文档内添加数据,然后在把abc.txt复制到F盘下
- Android原生AlertDialog使用总结
- codevs 1403 新三国争霸
- Longest Palindromic Substring
- c++虚函数
- 上海市及市内各区地方税务局联系地址与电话
- Makefile
- iOS10推送必看UNNotificationServiceExtension
- TCP/IP协议分层
- SharedPreferences使用讲解