如何更改Dialog的标题与按钮颜色详解

所属分类: 软件编程 / Android 阅读数: 39
收藏 0 赞 0 分享

前言

本文主要给大家介绍了如何更改Dialog的标题与按钮颜色的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

android.support.v7.app.AlertDialog

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

final AlertController mAlert;

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

修改按钮颜色

1. AlertDialog.getButton

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

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

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

传入对应的参数即可得到对应的Button

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

这种方式只能设置按钮的颜色,而无法设置标题颜色

2 AlertDialog.getWindow

AlertDialog的构造函数如下:

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

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也可以单独使用.

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Android studio点击跳转WebView详解

这篇文章主要为大家详细介绍了Android studio点击跳转WebView的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义Drawable实现圆形和圆角

这篇文章主要为大家详细介绍了Android自定义Drawable实现圆形和圆角,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义水平渐变进度条

这篇文章主要为大家详细介绍了Android自定义水平渐变进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ToolBar中menu无法同时显示图标和文字问题的解决方法

这篇文章主要为大家详细介绍了ToolBar中menu无法同时显示图标和文字问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解React Native监听Android回退按键与程序化退出应用

这篇文章主要介绍了详解React Native监听Android回退按键与程序化退出应用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

android实现上传本地图片到网络功能

这篇文章主要为大家详细介绍了android实现上传本地图片到网络功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现QQ登录功能

这篇文章主要为大家详细介绍了Android实现QQ登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简单的城市列表功能

这篇文章主要为大家详细介绍了Android实现简单的城市列表功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android Animation之TranslateAnimation(平移动画)

这篇文章主要为大家详细介绍了Animation之TranslateAnimation平移动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 中Failed to read key from keystore解决办法

这篇文章主要介绍了Android 中Failed to read key from keystore解决办法的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多