更改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也能夠單獨使用.

相關文章
相關標籤/搜索