AlertDialog源码解析之一
AlertDialog extends Dialog implements DialogInterface
public class Dialog implements DialogInterface, Window.Callback,
KeyEvent.Callback, OnCreateContextMenuListener, Window.OnWindowDismissedCallback、
static int resolveDialogTheme(Context context, int themeResId) { if (themeResId == THEME_TRADITIONAL) {return R.style.Theme_Dialog_Alert;} else if (themeResId == THEME_HOLO_DARK) {return R.style.Theme_Holo_Dialog_Alert;} else if (themeResId == THEME_HOLO_LIGHT) {return R.style.Theme_Holo_Light_Dialog_Alert;} else if (themeResId == THEME_DEVICE_DEFAULT_DARK) {return R.style.Theme_DeviceDefault_Dialog_Alert;} else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) {return R.style.Theme_DeviceDefault_Light_Dialog_Alert;} else if (themeResId >= 0x01000000) { // start of real resource IDs.return themeResId;} else {final TypedValue outValue = new TypedValue();context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true); return outValue.resourceId;}}
针对于AlertDialog属性中,如果没有去特殊的定义DialogTheme。它回去系统的themeResId。如果不是自定义的那么久对照系统中已经有的theme,然后再找到其中的dialog的样式。如果是自定义的就返回这个dialog的theme。所以这边可以看出Dialog的其中的属性和activity其中的属性并没有什么关联的地方,也就是如果在Dialog外层去定义属性比如textColor在Dialog中是不生效的。
Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {if (createContextThemeWrapper) {if (themeResId == 0) {final TypedValue outValue = new TypedValue();context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);themeResId = outValue.resourceId;} mContext = new ContextThemeWrapper(context, themeResId);} else { mContext = context;} mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final Window w = new PhoneWindow(mContext);mWindow = w;w.setCallback(this);w.setOnWindowDismissedCallback(this);w.setWindowManager(mWindowManager, null, null);w.setGravity(Gravity.CENTER); mListenersHandler = new ListenersHandler(this);}
这边是直接对Windows进行操作,可以看出来Dialog其实就是那个Activity上面在创建一个Windows.
AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,createContextThemeWrapper); mWindow.alwaysReadCloseOnTouchAttr();mAlert = new AlertController(getContext(), this, getWindow());}
public AlertController(Context context, DialogInterface di, Window window) { mContext = context;mDialogInterface = di;mWindow = window;mHandler = new ButtonHandler(di); final TypedArray a = context.obtainStyledAttributes(null,R.styleable.AlertDialog, R.attr.alertDialogStyle, 0); mAlertDialogLayout = a.getResourceId( R.styleable.AlertDialog_layout, R.layout.alert_dialog);mButtonPanelSideLayout = a.getResourceId( R.styleable.AlertDialog_buttonPanelSideLayout, 0);mListLayout = a.getResourceId( R.styleable.AlertDialog_listLayout, R.layout.select_dialog); mMultiChoiceItemLayout = a.getResourceId( R.styleable.AlertDialog_multiChoiceItemLayout,R.layout.select_dialog_multichoice);mSingleChoiceItemLayout = a.getResourceId( R.styleable.AlertDialog_singleChoiceItemLayout,R.layout.select_dialog_singlechoice);mListItemLayout = a.getResourceId( R.styleable.AlertDialog_listItemLayout,R.layout.select_dialog_item);//Add begin by meitu.zhanghong to init he mItemHeigh(180px) and mItemCount(6.5) mItemHeight =context.getResources().getDimensionPixelOffset(com.meitu.R.dimen.list_preferred_item_height_small);mItemCount = Utils.MAX_DIALOG_ITEM_MEITU;//Add enda.recycle();}
所以针对AlertDialog可以解析的attr有:
mWindow.setContentView(contentView);
public void setButtonPanelLayoutHint(int layoutHint) { mButtonPanelLayoutHint = layoutHint;}
private int selectContentView() {if (mButtonPanelSideLayout == 0) {return mAlertDialogLayout;}if (mButtonPanelLayoutHint == AlertDialog.LAYOUT_HINT_SIDE) {return mButtonPanelSideLayout;}// TODO: use layout hint side for long messages/listsreturn mAlertDialogLayout;}
如果是存在提示说明的情况下,回去使用,mButtonPanelSideLayout 替换mAlertDialogLayout文件。
public void installContent() {/* We use a custom title so never request a window title */mWindow.requestFeature(Window.FEATURE_NO_TITLE); int contentView = selectContentView();mWindow.setContentView(contentView);setupView();setupDecor();}
关注:setupView()
private void setupView() { P.mCancelable = false; ......//取到不同的控件 setupContent(contentPanel);//对于alert_dialog.xml文件中 scrollview listview 的不同处理方式 setupButtons(buttonPanel);//对于三个Button,在系统中定义了三个button的样式分别是
//int BIT_BUTTON_POSITIVE = 1; 确认按钮 //int BIT_BUTTON_NEGATIVE = 2;//取消按钮 //int BIT_BUTTON_NEUTRAL = 4;中间的按钮 可以通过重写这三个样式在style.xml文件中去改变button的样式
setupTitle(topPanel);// 对于自定义title,有icon的title,只有textview title的不同处理方式。 ...... // Only display the text spacer if we don't have buttons. if (!hasButtonPanel) { if (contentPanel != null) { final View spacer = contentPanel.findViewById(R.id.textSpacerNoButtons); if (spacer != null) { spacer.setVisibility(View.VISIBLE); } } mWindow.setCloseOnTouchOutsideIfNotSet(true);//点击空白区域是否消失的初始化,针对AlertDialog的一个属性 P.mCancelable = false; }
......
final TypedArray a = mContext.obtainStyledAttributes( null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0); setBackground(a, topPanel, contentPanel, customPanel, buttonPanel, hasTopPanel, hasCustomPanel, hasButtonPanel); a.recycle(); // Add begin by meitu.liujr for:MEIOS3 if(com.meitu.mobile.theme.Utils.isMeituUI(mContext)){ WindowManager.LayoutParams lp = mWindow.getAttributes(); if(mListView !=null){ if(mTitleView!=null){ mTitleView.setTextColor(mContext.getResources().getColor(com.meitu.internal.R.color.primary_text_default_material_light)); mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mContext.getResources().getDimensionPixelSize(com.meitu.internal.R.dimen.text_size_large_material)); } // has not buttons if(TextUtils.isEmpty(mButtonNegativeText) && TextUtils.isEmpty(mButtonPositiveText) && TextUtils.isEmpty(mButtonNeutralText)){ //if( !hasButtonPanel){ lp.gravity = Gravity.BOTTOM; lp.windowAnimations = android.R.style.Animation_InputMethod; // has title if(mCustomTitleView !=null || !TextUtils.isEmpty(mTitle)){ // if(hasTopPanel){ mWindow.setBackgroundDrawableResource(com.meitu.internal.R.drawable.dialog_background_meitu_title); }else{ mWindow.setBackgroundDrawableResource(com.meitu.internal.R.drawable.menu_hardkey_panel_meitu_light); } }else{ lp.gravity = Gravity.CENTER; lp.windowAnimations = android.R.style.Animation_Dialog; mWindow.setBackgroundDrawableResource(com.meitu.internal.R.drawable.dialog_background_meitu); } }else{ if(mTitleView!=null){ mTitleView.setTextColor(mContext.getResources().getColor(com.meitu.internal.R.color.dialog_title_text_color)); mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mContext.getResources().getDimensionPixelSize(com.meitu.internal.R.dimen.text_size_subhead_material)); } lp.gravity = Gravity.CENTER; lp.windowAnimations = android.R.style.Animation_Dialog; mWindow.setBackgroundDrawableResource(com.meitu.internal.R.drawable.dialog_background_meitu); } mWindow.setAttributes(lp); } // Add end}
AlertDialog源码解析之一相关推荐
- Android源码解析--AlertDialog及AlertDialog.Builder
昨天晚上弄到很晚,简单的看了下Dialog的源码,说要分析下建造者模式,在dialog里面的应用其实是在AlertDialog中. 按照惯例,先看类说明: [java] view plaincopy ...
- 谷歌BERT预训练源码解析(二):模型构建
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_39470744/arti ...
- 谷歌BERT预训练源码解析(三):训练过程
目录 前言 源码解析 主函数 自定义模型 遮蔽词预测 下一句预测 规范化数据集 前言 本部分介绍BERT训练过程,BERT模型训练过程是在自己的TPU上进行的,这部分我没做过研究所以不做深入探讨.BE ...
- 谷歌BERT预训练源码解析(一):训练数据生成
目录 预训练源码结构简介 输入输出 源码解析 参数 主函数 创建训练实例 下一句预测&实例生成 随机遮蔽 输出 结果一览 预训练源码结构简介 关于BERT,简单来说,它是一个基于Transfo ...
- Gin源码解析和例子——中间件(middleware)
在<Gin源码解析和例子--路由>一文中,我们已经初识中间件.本文将继续探讨这个技术.(转载请指明出于breaksoftware的csdn博客) Gin的中间件,本质是一个匿名回调函数.这 ...
- Colly源码解析——结合例子分析底层实现
通过<Colly源码解析--框架>分析,我们可以知道Colly执行的主要流程.本文将结合http://go-colly.org上的例子分析一些高级设置的底层实现.(转载请指明出于break ...
- libev源码解析——定时器监视器和组织形式
我们先看下定时器监视器的数据结构.(转载请指明出于breaksoftware的csdn博客) /* invoked after a specific time, repeatable (based o ...
- libev源码解析——定时器原理
本文将回答<libev源码解析--I/O模型>中抛出的两个问题.(转载请指明出于breaksoftware的csdn博客) 对于问题1:为什么backend_poll函数需要指定超时?我们 ...
- libev源码解析——I/O模型
在<libev源码解析--总览>一文中,我们介绍过,libev是一个基于事件的循环库.本文将介绍其和事件及循环之间的关系.(转载请指明出于breaksoftware的csdn博客) 目前i ...
最新文章
- mysql M/S配置小记
- 【 FPGA 】四位16进制的数码管动态显示设计
- linux安装ppp服务器,服务器_最简单的Linux系统上的pptpd服务器安装,1. rpm -qa|grep ppp 检查ppp版本, - phpStudy...
- 通过keras例子理解LSTM 循环神经网络(RNN)
- 结合vue、react、angular谈谈MVC、MVP、MVVM框架
- Experimental-work1
- sublime:查看二进制文件
- 元宵节正月十五|设计师正需要的图片素材看这里
- ie对象不支持“jggrid“属性或方法_8.2 location 对象
- 斯蒂夫乔布斯传札记:第九波
- SVN创建分支/合并分支/切换分支
- lol8月21号服务器维护,lol维护到几点今天?英雄联盟LOL8月21日维护更新内容
- fiddler4在win7抓取https的配置整理
- SOUI GDI+渲染引擎下的字体特效,抛砖引玉
- 【科研】浅学Cross-attention?
- PHP防止vip视频被下载
- 移动端如何在前端阻止input框获得焦点时手机键盘的弹出
- 2.3.1-4. IEEE 754 标准
- 《月之猎人 (Moon Hunters)》主角设计
- Zemax 2023安装教程
热门文章
- 为什么微型芯片是机器学习的命门?
- 第 3 章 MybatisPlus 注入 SQL 原理分析
- java程序设计是选修课_Java程序设计_中国大学 MOOC_章节考试选修课答案
- python 翻译库本地库_利用python爬取并翻译GEO数据库
- 数据增强_NLP 数据增强方法 EDA
- jquert ajax文件 mvc,jquery ajax file upload NET MVC 无刷新文件上传
- gromacs 安装_GROMACS:粗粒化力场建立和模拟上线!
- Promise的deferred对象详解
- smoothl1函数_Faster RCNN的损失函数(Loss Function)
- 计算机c语言知识点txt,计算机二级C语言(重要知识点)