乍一看视觉效果还可以,但当我们想改变一下标题的文字颜色或者按钮颜色应该怎么做呢?

public class AlertDialog extends Dialog implements DialogInterface {
    private AlertController mAlert;
    ......

AlertDialog类中并没有直接定义控件,而是通过AlertController类来设置各控件的属性。
然而在android studio中无法直接查看该类源码,因为部分源码的标签是@hide的,即使文件存在但编辑器也不会显示,所以需要通过另外的软件来查看源码。

这里我使用了Source Insight,大家可从此处下载:Source Insight 4.0 最简单的破解安装

Source Insight的使用教程:使用 Source InSight 阅读 Android 源码

有条件的朋友还可以在AndroidXRef网站上直接查看,里面有各个版本的Android源码,但我因为访问速度太慢就没用这个看。

回到正题,在AlertController类中,可以看到以下变量:

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

但由于我们无法直接访问这些变量,回想起上学期学习的高级Java,其中学到的反射机制终于可以派上用场了!

  • 修改文字颜色、大小
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("标题")
                .setMessage("内容")
                .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(你要设置的颜色);
            mTitleView.setTextSize(你要设置的文字大小);
            Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
            mTitle.setAccessible(true);
            TextView mTitleView = (TextView) mTitle.get(mAlertController);
            mTitleView.setTextColor(你要设置的颜色);
            mTitleView.setTextSize(你要设置的文字大小);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
  • 修改按钮颜色
    同样,我们可以通过反射机制修改,也可以直接使用AlertDialog类中提供的方法:
* Gets one of the buttons used in the dialog. Returns null if the specified * button does not exist or the dialog has not yet been fully created (for * example, via {@link #show()} or {@link #create()}). * @param whichButton The identifier of the button that should be returned. * For example, this can be * {@link DialogInterface#BUTTON_POSITIVE}. * @return The button from the dialog, or null if a button does not exist. public Button getButton(int whichButton) { return mAlert.getButton(whichButton);
AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("标题")
                .setMessage("内容")
                .setPositiveButton("确定",null)
                .setNegativeButton("取消",null)
                .create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(你要设置的颜色);
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextSize(你要设置的文字大小);

附上一张修改后的效果图:

至此,我们就可以用反射机制修改Dialog中各控件的各个属性(又如gravity等,但经过实践发现Title无法设置Gravity属性???Message却又可以???暂留一个未解之谜),需要用到的时候再探索就好了。

反射方法必须在dialog.show()方法后执行,不然会报错。

作为程序员,要想让组件有与众不同的效果,必须学会编写自定义View。 这次我就先从自定义Dialog中组件的基本属性开始学习。一个最基本的Dialog:乍一看视觉效果还可以,但当我们想改变一下标题的文字颜色或者按钮颜色应该怎么做呢?查看源码public class AlertDialog extends Dialog implements DialogInterf...
网上流传的方法是在应用程序类的InitInstance()函数中添加: //设置对话框背景和文本颜色 SetDialogBkColor(RGB(160,180,220),RGB(0,0,0));   但是SetDialogBkColor中VS 2003中已经不支持了,只有在VC中支持。   在VS中的办法就是响应WM_CTLCOLOR消息,在消息映射函数中添加: HBRUSH
要想修改CButton类按钮背景颜色文字颜色,必须利用自绘方法对按钮进行重新绘制。这可以通过定义一个以CButton为基类的新按钮类来实现。以下为具体的实现方法: 加入一个新类,类名:CMyButton,基类:CButton。 在头文件 MyButton.h 中加入以下变量和函数定义: private:      int          m_Style;  
编程环境:VS2013,MFC MFC的button控件是一个不同于其他控件,其CButtton类是CWnd的一个子类,在修改button的背景、颜色和边框的时候必须自己进行编写一个新的类,如CMyButton。下面是进行背景、颜色、边框修改的步骤: 1.在项目->添加类->CMyButton。这样会自动生成两个文件,一个.CPP文件和一个.h文件。例如CMyButton.cpp和...
可以直接调用,操作方便,调用代码如下: jConfirm('Can you confirm this,<span am red.?', 'Confirmation Dialog', function(r) { this.$confirm(`费用项<span style='color:#0096ff'>${res.data.data.expenseItem}</span>已经在该期间维护费用,是否继续维护。`, '提示',{ confirmButtonText: '确定',
像是这种弹出框我们一般很少去做文字样式修改,但是也免不了要修改 现在就是这样,有一个需求是要做下样式的修改 通常我们都是这样写,但是当需要修改文本样式的时候,我们需要 createElement 新建元素和对象,然后对新建的元素进行标签化设置 this.$confirm('内容提示',{ type:'warning' .then(()=>{ done() .catch(()=>{}) 修改文本样式代码,如果需要 icon 图标,需要把注释代码打开,其中 colorYellow
// v-dialogDrag: 弹窗拖拽 Vue.directive('dialogDrag', { bind(el, binding, vnode, oldVnode) { const dialogHeaderEl = el.querySelector('.el-dialog__header') const dragDom = el.querySe
你可以通过设置el-dialog组件的height属性来改变其高度,例如: <el-dialog :visible.sync="dialogVisible" :title="title" :height="500px"> <p>这是一段内容</p> </el-dialog> 在上面的代码中,将el-dialog组件的高度设置为500px。你也可以根据需要调整高度。
Glide从网络加载图片报错:class com.bumptech.glide.load.engine.GlideException: Failed to load resource 虚青海er: 方法过时了,大佬知道现在遇到这种问题怎么处理吗 Ubuntu 16.04——配置Nginx及Https服务 Tisfy: Nice! Android Studio之回退Gradle版本方法 (Minimum supported Gradle version is 4.10.1. Current version is 4.6.) Mybatis:There is no getter for property named 'xxx' in 'class java.lang.Integer/String...' 最详细的Nginx+Tomcat+Https配置教程(阿里云+Symantec证书)