资讯专栏INFORMATION COLUMN

更改Dialog的标题和按钮颜色

JiaXinYi / 1203人阅读

摘要:在这个类中第一行就定义了如下变量的功能的具体实现都在这个内部封装修改按钮颜色这里的参数有三种类型传入对应的参数即可得到对应的这种方式只能设置按钮的颜色而无法设置标题颜色需要注意的是这个方法必须在或者方法之后调用查看官方注释的构造函数如下这里

android.support.v7.app.AlertDialog

在这个类中第一行就定义了如下变量:

final AlertController mAlert;

AlertDialog的功能的具体实现都在这个AlertController内部封装.

修改按钮颜色 1. AlertDialog.getButton
public Button getButton(int whichButton) {
        return mAlert.getButton(whichButton);
    }

这里的参数whichButton有三种类型:

DialogInterface.BUTTON_POSITIVE

DialogInterface.BUTTON_NEGATIVE

DialogInterface.BUTTON_NEUTRAL

传入对应的参数即可得到对应的Button

Button btnPositive = (Button)AlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setTextColor(color);

这种方式只能设置按钮的颜色,而无法设置标题颜色

需要注意的是这个方法必须在AlertDialog.show()或者AlertDialog.create()方法之后调用
查看官方注释

/**
     * 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);
    }
2 AlertDialog.getWindow

AlertDialog的构造函数如下:

protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, resolveDialogTheme(context, themeResId));
        mAlert = new AlertController(getContext(), this, getWindow());
    }

这里初始化了AlertController,并传入了getWindow(),这个getWindow()是AlertDialog继承自Dialog的方法.方法如下:

#Dialog.getWindow()
 public @Nullable Window getWindow() {
        return mWindow;
    }

将这个window对象传入AlertController后,在AlertController源码中可以看到对话框标题和按钮的id,并通过Window.findViewById(id)获取对应的View.
所以这里可以这样得到对话框的标题和按钮:

//标题
TextView tvTitle = (TextView)AlertDialog.getWindow().findViewById(R.id.alertTitle);
//按钮
Button btnPositive = (Button)AlertDialog.getWindow().findViewById(R.id.button1);

然后设置所需要的颜色就可以了.这种方法可以修改Dialog的所有设置了id的控件的字体颜色.

3 反射

3.1 首先拿到AlertController对象

 Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
 mAlert.setAccessible(true);
 Object controller =  mAlert.get(dialog);

在AlertController内部查找到需要更改字体颜色的标题和按钮

Button mButtonPositive;
Button mButtonNegative;
Button mButtonNeutral;
private TextView mTitleView;
private TextView mMessageView;
然后通过反射获取对应控件,修改控件颜色即可
  Field mTitleView = controller.getClass().getDeclaredField("mTitleView");
  mTitleView.setAccessible(true);
  TextView tvTitle = (TextView) mTitleView.get(controller);
  tvTitle.setTextColor(Color.GREEN);//更改标题的颜色
三种方式比较起来,第二种是最简单,效率也是最高的 更改Dialog显示的位置
Window window = dialog.getWindow();
 WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.x = 100;
lp.y = 100;
window.setAttributes(lp);

这里要注意的是,WindowManager.LayoutParams的x和y坐标,看源码注释如下:

  /**
         * X position for this window.  With the default gravity it is ignored.
         * When using {@link Gravity#LEFT} or {@link Gravity#START} or {@link Gravity#RIGHT} or
         * {@link Gravity#END} it provides an offset from the given edge.
         */
        @ViewDebug.ExportedProperty
        public int x;

        /**
         * Y position for this window.  With the default gravity it is ignored.
         * When using {@link Gravity#TOP} or {@link Gravity#BOTTOM} it provides
         * an offset from the given edge.
         */
        @ViewDebug.ExportedProperty
        public int y;

如果lp.gravity是默认的,那么x和y即使设置了也是无效的.因此x和y需要和lp.gravity搭配使用才有效果.当然lp.gravity也可以多带带使用.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70241.html

相关文章

  • pyqt5——对话框

    摘要:这个示例有一个按钮和一个输入框,点击按钮显示对话框,输入的文本会显示在输入框里。把得到的字符串放到输入框里。我们创建了一个有一个按钮和一个标签的的对话框,我们可以使用这个功能修改字体样式。 对话框 对话框是一个现代GUI应用不可或缺的一部分。对话是两个人之间的交流,对话框就是人与电脑之间的对话。对话框用来输入数据,修改数据,修改应用设置等等。 输入文字 QInputDialog提供了一...

    Me_Kun 评论0 收藏0
  • 自定义弹窗(dialog

    摘要:构造函数的原型方法合并自定义参数移除已存在的弹窗原型中提供了和方法。方法可以接受参数,也是一个参数对象,与在构造函数中的方法一样,主要是为了方便更改弹窗功能。 简易弹窗 开发说明 项目使用原型对象的方式实现弹窗的基本功能 项目依赖jquery,如果使用zepto,可能需要改动代码,未测试,有问题请反馈,及时解决 项目提供了可定制弹窗,可以自定义按钮,标题,以及对应按钮的回调函数 样式...

    ctriptech 评论0 收藏0
  • 自定义弹窗(dialog

    摘要:构造函数的原型方法合并自定义参数移除已存在的弹窗原型中提供了和方法。方法可以接受参数,也是一个参数对象,与在构造函数中的方法一样,主要是为了方便更改弹窗功能。 简易弹窗 开发说明 项目使用原型对象的方式实现弹窗的基本功能 项目依赖jquery,如果使用zepto,可能需要改动代码,未测试,有问题请反馈,及时解决 项目提供了可定制弹窗,可以自定义按钮,标题,以及对应按钮的回调函数 样式...

    BigNerdCoding 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<