如何更改Dialog的標(biāo)題與按鈕顏色詳解
前言
本文主要給大家介紹了如何更改Dialog的標(biāo)題與按鈕顏色的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
android.support.v7.app.AlertDialog
在這個(gè)類中第一行就定義了如下變量:
final AlertController mAlert;
AlertDialog的功能的具體實(shí)現(xiàn)都在這個(gè)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
傳入對(duì)應(yīng)的參數(shù)即可得到對(duì)應(yīng)的Button
Button btnPositive = (Button)AlertDialog.getButton(DialogInterface.BUTTON_POSITIVE); btnPositive.setTextColor(color);
這種方式只能設(shè)置按鈕的顏色,而無(wú)法設(shè)置標(biāo)題顏色
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() ,這個(gè)getWindow()是AlertDialog繼承自Dialog的方法.方法如下:
#Dialog.getWindow()
public @Nullable Window getWindow() {
return mWindow;
}
將這個(gè)window對(duì)象傳入AlertController后,在AlertController源碼中可以看到對(duì)話框標(biāo)題和按鈕的id,并通過(guò)Window.findViewById(id)獲取對(duì)應(yīng)的View.
所以這里可以這樣得到對(duì)話框的標(biāo)題和按鈕:
//標(biāo)題 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對(duì)象
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object controller = mAlert.get(dialog);
在AlertController內(nèi)部查找到需要更改字體顏色的標(biāo)題和按鈕
Button mButtonPositive; Button mButtonNegative; Button mButtonNeutral; private TextView mTitleView; private TextView mMessageView;
然后通過(guò)反射獲取對(duì)應(yīng)控件,修改控件顏色即可
Field mTitleView = controller.getClass().getDeclaredField("mTitleView");
mTitleView.setAccessible(true);
TextView tvTitle = (TextView) mTitleView.get(controller);
tvTitle.setTextColor(Color.GREEN);//更改標(biāo)題的顏色
三種方式比較起來(lái),第二種是最簡(jiǎn)單,效率也是最高的
更改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坐標(biāo),看源碼注釋如下:
/**
* 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是默認(rèn)的,那么x和y即使設(shè)置了也是無(wú)效的.因此x和y需要和lp.gravity搭配使用才有效果.當(dāng)然lp.gravity也可以單獨(dú)使用.
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android Studio 3.6 調(diào)試 smali的全過(guò)程
這篇文章主要介紹了Android Studio 3.6 調(diào)試 smali, 目前最新版的 Android Studio 利用附加功能調(diào)試 smali 非常方便,具體操作步驟跟隨小編一起看看吧2020-02-02
android studio如何通過(guò) jni 調(diào)用第三方非標(biāo)準(zhǔn) so庫(kù)
這篇文章主要介紹了android studio如何通過(guò) jni 調(diào)用第三方非標(biāo)準(zhǔn) so庫(kù),調(diào)用第三方的so方法,但這個(gè)so內(nèi)的方法不是標(biāo)準(zhǔn)的jni方法,這就需要我們自己寫jni然后鏈接到第三方so庫(kù),通過(guò)jni調(diào)用so庫(kù)中的方法,需要的朋友可以參考下2025-04-04
Android小知識(shí)之圖片的3種壓縮方式小結(jié)
這篇文章主要給大家介紹了關(guān)于Android小知識(shí)之圖片的3種壓縮方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android之RecyclerView實(shí)現(xiàn)時(shí)光軸效果示例
本篇文章主要介紹了Android之RecyclerView實(shí)現(xiàn)時(shí)光軸效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
解析android 流量監(jiān)測(cè)的實(shí)現(xiàn)原理
本篇文章是對(duì)android中流量監(jiān)測(cè)的實(shí)現(xiàn)原理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android提高之BroadcastReceiver實(shí)例詳解
這篇文章主要介紹了Android的BroadcastReceiver用法,在Android的項(xiàng)目開發(fā)中是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android 抽屜效果的導(dǎo)航菜單實(shí)現(xiàn)代碼實(shí)例
本篇文章主要介紹了Android 抽屜效果的導(dǎo)航菜單實(shí)現(xiàn)代碼實(shí)例,這種側(cè)滑的抽屜效果的菜單很好,有興趣的可以了解一下。2016-12-12

