更改Dialog的标题和按钮颜色

android.support.v7.app.AlertDialog

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

final AlertController mAlert;

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

修改按钮颜色

1. AlertDialog.getButton

public Button getButton(int whichButton) {
        return mAlert.getButton(whichButton);
    }

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

  • 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()方法以后调用
查看官方注释this

/**
     * 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的构造函数以下:code

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的控件的字体颜色.ci

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也能够单独使用.

相关文章
相关标签/搜索