欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何更改Dialog的標題與按鈕顏色詳解

 更新時間:2017年08月27日 16:36:59   作者:idealcn  
這篇文章主要給大家介紹了關(guān)于如何更改Dialog的標題與按鈕顏色的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考借鑒,下面跟著小編來一起學習學習吧。

前言

本文主要給大家介紹了如何更改Dialog的標題與按鈕顏色的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

android.support.v7.app.AlertDialog

在這個類中第一行就定義了如下變量:

final AlertController mAlert;

AlertDialog的功能的具體實現(xiàn)都在這個AlertController內(nèi)部封裝.

修改按鈕顏色

1. AlertDialog.getButton

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

這里的參數(shù)whichButton有三種類型:

  • DialogInterface.BUTTON_POSITIVE
  • DialogInterface.BUTTON_NEGATIVE
  • DialogInterface.BUTTON_NEUTRAL

傳入對應(yīng)的參數(shù)即可得到對應(yīng)的Button

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

這種方式只能設(shè)置按鈕的顏色,而無法設(shè)置標題顏色

2 AlertDialog.getWindow

AlertDialog的構(gòu)造函數(shù)如下:

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)獲取對應(yīng)的View.

所以這里可以這樣得到對話框的標題和按鈕:

//標題
TextView tvTitle = (TextView)AlertDialog.getWindow().findViewById(R.id.alertTitle);
//按鈕
Button btnPositive = (Button)AlertDialog.getWindow().findViewById(R.id.button1);

然后設(shè)置所需要的顏色就可以了.這種方法可以修改Dialog的所有設(shè)置了id的控件的字體顏色.

3 反射

3.1 首先拿到AlertController對象

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

在AlertController內(nèi)部查找到需要更改字體顏色的標題和按鈕

Button mButtonPositive;
Button mButtonNegative;
Button mButtonNeutral;
private TextView mTitleView;
private TextView mMessageView;

然后通過反射獲取對應(yīng)控件,修改控件顏色即可

 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即使設(shè)置了也是無效的.因此x和y需要和lp.gravity搭配使用才有效果.當然lp.gravity也可以單獨使用.

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論