如何更改Dialog的標題與按鈕顏色詳解
前言
本文主要給大家介紹了如何更改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)文章
AndroidView與Compose框架交互實現(xiàn)介紹
Android Compose自推出正式版本后,google 就一直推薦使用Compose來開發(fā)。正好疫情期間,作為一個 Android 摸魚達人,就來摸索一下Compose的開發(fā)。說實話開發(fā)了2天感覺對Android 開發(fā)人員來說變化是巨大的,但是作為從業(yè)者我們還必須學習和學會,才能不被甩開2022-09-09Android 中CheckBox多項選擇當前的position信息提交的示例代碼
這篇文章主要介紹了Android 中CheckBox多項選擇當前的position信息提交的示例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-07-07Android種使用Notification實現(xiàn)通知管理以及自定義通知欄實例(示例四)
本篇文章主要介紹了Android種使用Notification實現(xiàn)通知管理以及自定義通知欄實例,具有一定的參考價值,需要的朋友可以了解一下。2016-12-12android開發(fā)教程之a(chǎn)ndroid的handler使用方法
這篇文章主要介紹了android的handler使用方法,大家參考使用吧2014-01-01詳談Android動畫效果translate、scale、alpha、rotate
下面小編就為大家?guī)硪黄斦凙ndroid動畫效果translate、scale、alpha、rotate。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01