AlertDialog修改title,message,button样式
2016-05-13 15:52
1031 查看
我们一直在用系统的Dialog,但是我们想把其中的一些显示风格换成我们自己的,比如,我们需要改变Dialog的title,message,或者button的颜色,以及字体。那么我们应该怎么做呢? 一般,我们在设置title,message的时候,并没有setTitleSize,setMessageSize的方法供我们来调用,这个时候怎么办呢,由于title在这其中有点特殊,我们先说message和button。
我们需要将整个Dialog展示出来,即,初始化后设置各种属性,然后调用show()去展示AlertDialog dlgShowBack = new AlertDialog.Builder(this).create(); dlgShowBack.setTitle("提示"); dlgShowBack.setMessage("您确定要退出?"); dlgShowBack.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dlgShowBack.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgShowBack.show();
2,在Dilaog初始化完成之后,再来获取它的一些属性进行设置。 message我们可以通过android.R.id.message来获取。即:
((TextView)findViewById(android.R.id.message)).setTextSize(17);
button我们可以以下方式来获取,然后进行设置。
Button btnPositive = dlgShowBack.getButton(android.app.AlertDialog.BUTTON_POSITIVE); Button btnNegative = dlgShowBack.getButton(android.app.AlertDialog.BUTTON_NEGATIVE); btnNegative.setTextColor(getResources().getColor(R.color.colorPrimary)); btnNegative.setTextSize(18); btnPositive.setTextColor(getResources().getColor(R.color.colorPrimary)); btnPositive.setTextSize(18);
注意:这里这些属性的获取都一定是要在Dialog调用完show()方法之后,即Dialog展示出来之后。要不就会NullPointException。
3,title的设置 title在这里是比较特殊的,它和message一样,有android.R.id.title,但是这样获取到的title为null,因为Android系统是不允许我们来修改title的,那么怎么办呢?我们可以在setTitle()之前对title内容的属性进行设置。
SpannableStringBuilder Builder = new SpannableStringBuilder(getString(R.string.title_dlg_delete)); StyleSpan spanState1 = new StyleSpan(Typeface.BOLD); Builder.setSpan(spanState1,0,getString(R.string.title_dlg_delete).length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); dlgShowBack.setTitle(Builder);
这样就改变了title的字体。
4,最后来张效果图。
相关文章推荐
- [转载]Activity中ConfigChanges属性的用法
- android之定时器AlarmManager
- android上改变listView的选中颜色
- DWZ刷新dialog解决方法
- JQuery插件Style定制化方法的分析与比较
- 为了防止乱码,请将用于页面编码的meta charset放到title的前面
- HTML实现title 属性换行小技巧
- Winform消除button按下出现的虚线简单实现方法
- Android编程之Button控件用法实例分析
- 用jquery模仿的a的title属性的例子
- ASP.NET 中 Button、LinkButton和ImageButton 三种控件的使用详解
- CSS和JS标签style属性对照表(方便js开发的朋友)
- Jquery修改页面标题title其它JS失效的解决方法
- ppk谈JavaScript style属性
- MC Dialog js弹出层 完美兼容多浏览器(5.6更新)
- js Dialog 去掉右上角的X关闭功能
- jquery BS,dialog控件自适应大小
- jQuery EasyUI Dialog拖不下来如何解决
- jquery ui dialog实现弹窗特效的思路及代码
- JQUERY dialog的用法详细解析