Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等_第1张图片

  private void showAlerDialog() {        AlertDialog dialog = new AlertDialog.Builder(this)                .setTitle("AlerDialog")                .setMessage("这是一个AlertDialog")                .setPositiveButton("确定",null)                .setNegativeButton("取消",null)                .create();        dialog.show();    }

需求

我们在android中可以很容易的通过上面的代码弹出一个AlertDialog,其中的标题,内容还有按钮的颜色大小等,系统代码中并没有暴露出方法来允许我们自定义他们,可假如有需求,为了突出确定,需要将确定的按钮修改为红色,我们该怎么做呢,或者是需要修改标题颜色等,当然我们可以选择自定义View,在此处我们不讨论这个方法,我们尝试修改原生的AlertDialog的标题,按钮颜色等;

分析

修改内容颜色

Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等_第2张图片

假如我们需要修改内容的颜色为蓝色,我们该如何修改呢?既然原生的AlertDialog没有提供修改的方法,那我们可以通过反射来提取到这个控件,然后修改其颜色即可;

public class AlertDialog extends AppCompatDialog implements DialogInterface {    final AlertController mAlert;    ...}

点开AlertDialog源码我们发现有一个全局的AlertControllerAlertDialog主要就是通过这个类来实现的,我们继续看这个类的源码;

class AlertController {    ...    private ImageView mIconView;    private TextView mTitleView;    private TextView mMessageView;    private View mCustomTitleView;   ...

在这个类的源码中我们看到了有mTitleViewmMessageView等字段,这些字段就是我们所需要的,我们就可以通过反射来动态修改他们;

private void showAlerDialog() {        AlertDialog dialog = new AlertDialog.Builder(this)                .setTitle("AlerDialog")                .setMessage("这是一个AlertDialog")                .setPositiveButton("确定",null)                .setNegativeButton("取消",null)                .create();        dialog.show();        try {            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");            mAlert.setAccessible(true);            Object mAlertController = mAlert.get(dialog);            Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");            mMessage.setAccessible(true);            TextView mMessageView = (TextView) mMessage.get(mAlertController);            mMessageView.setTextColor(Color.BLUE);        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (NoSuchFieldException e) {            e.printStackTrace();        }    }

通过上面的代码,我们就可以通过反射来修改原生AlertDialog中内容的颜色或者大小;

修改按钮颜色

Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等_第3张图片

修改按钮的颜色同样可以通过反射的方法来完成,不过原生的AlertDialog提供了相应的方法来实现针对按钮的操作,所以我们可以通过以下方法直接调用,例如将按钮的颜色一个修改为黑色,一个修改为蓝色:

  private void showAlerDialog() {        AlertDialog dialog = new AlertDialog.Builder(this)                .setTitle("AlerDialog")                .setMessage("这是一个AlertDialog")                .setPositiveButton("确定",null)                .setNegativeButton("取消",null)                .create();        dialog.show();        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);    }

通过以上代码,就可以将AlertDialog中按钮的颜色来自己定义;

这样我们就完成了自定义标题,内容,按钮颜色或者大小等,需要注意的是,不管是通过反射,还是原生的方法来修改,都需要在调用AlertDialogshow()方法后进行,否则会报错;

更多相关文章

  1. android把字符串内容保存到指定路径
  2. android仿苹果分段按钮
  3. Android中内容提供者ContentProvider实现数据库增删改查
  4. FloatingActionButton 浮动按钮
  5. android 设置键盘按钮为发送按钮并监听 及 键盘显示与隐藏 监听
  6. android 实现自由移动的悬浮按钮
  7. Android的supportV7中默认按钮的颜色设置
  8. android studio 添加按钮点击事件的三种方法

随机推荐

  1. 强烈推荐转载-Android 性能测试
  2. android 手动实现可输入下拉框Spinner控
  3. 【Android 内存优化】Bitmap 图像尺寸缩
  4. 当前十分主流的几款Android桌面启动器推
  5. Android Socket通信介绍和实例
  6. Android XML解析学习——Pull方式
  7. 代码混淆—android被反编译的两种解决方
  8. Android零基础教程8天学会移动开发
  9. H5 通过 input 标签,调起 Android 手机相
  10. Android的开机启动流程概述