今天,简单讲讲android 中的



DecorView的使用。

getWindow().getDecorView()的方法可以获取到decorView,decorView是什么呢?
decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。

  Rect rect = new Rect();  /* * getWindow().getDecorView()得到的View是Window中的最顶层View,可以从Window中获取到该View, * 然后该View有个getWindowVisibleDisplayFrame()方法可以获取到程序显示的区域, * 包括标题栏,但不包括状态栏。 */  getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); 1.获取状态栏高度:  根据上面所述,我们可以通过rect对象得到手机状态栏的高度 int statusBarHeight = rect.top; 2.获取标题栏高度: getWindow().findViewById(Window.ID_ANDROID_CONTENT); 该方法获取到的View是程序不包括标题栏的部分,这样我们就可以计算出标题栏的高度了。 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();    //statusBarHeight是上面所求的状态栏的高度    int titleBarHeight = contentTop - statusBarHeight   

接下来举一个具体的例子:

先来看看实现的效果

实现的大致思路

  1. 首先需要明白什么是DecorView,他是android中界面的根布局。其实android的activity界面整个就是一个控件树,DecorView是根节点,DecorView的孩子节点就是一个LinearLayout,这个LinearLayout的孩子系节点就包括状态栏 + 和我们自己写的布局
  2. DecorView是FramLayout的子类(可以从源码中看到)
  3. 既然DecorView是根节点,而且还是FrameLayout,所以我们可以把我们自己的布局 添加到DecorView 或者 从DecorView移除,这样就模拟出了一个Dialog的效果~~ ,当然这个Dialog的样式,动画就可以自己想怎么写就怎么写了撒
  4. 通过activity.getWindow().getDecorView()可以获得DecorView

[下面大量 代码 ]

第一个对话框的实现

public class TipsDialog {private Activity activity;private View rootView;private TextView confirmTextView;private TextView cancelTextView;private TextView contentTextView;private boolean isShowing;public TipsDialog(Activity activity) {this.activity = activity;isShowing = false;rootView = LayoutInflater.from(activity).inflate(R.layout.view_tips_dialog,null);confirmTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_confirm);cancelTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_cancel);contentTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_content);}public void show(){if(activity == null){return;}if(isShowing){return;}ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);params.gravity = Gravity.CENTER;rootView.setLayoutParams(params);decorView.addView(rootView);rootView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});RotateAnimation rotateAnimation = new RotateAnimation(0,720f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(2000);contentTextView.startAnimation(rotateAnimation);isShowing = true;}public void dismiss(){if(!isShowing){return;}isShowing = false;if(rootView.getParent() == null){return;}ViewGroup parent = (ViewGroup) rootView.getParent();parent.removeView(rootView);}public int getRandomColor(){Random random = new Random();return Color.argb(random.nextInt(200),random.nextInt(240),random.nextInt(240),random.nextInt(240));}public boolean isShowing() {return isShowing;}
}

其实就是show的时候将布局添加到DecorView上面去,dismiss的时候将布局从DecorView上面移除

提示的实现(没有处理完善~~ 仅仅就是说明哈DecorView)

public class TopTipDialog {private Activity activity;private View rootView;private boolean isShowing;private static final int VIEW_HEIGHT = 64;//pxpublic TopTipDialog(Activity activity) {this.activity = activity;rootView = LayoutInflater.from(activity).inflate(R.layout.view_top_tip_dialog,null);}public void show(){if(isShowing){return;}ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, VIEW_HEIGHT);params.gravity = Gravity.TOP;params.setMargins(0,0,0,-VIEW_HEIGHT);rootView.setLayoutParams(params);TranslateAnimation translateAnimation = new TranslateAnimation(0,0,-VIEW_HEIGHT,0);translateAnimation.setDuration(1500);translateAnimation.setFillAfter(true);decorView.addView(rootView);rootView.startAnimation(translateAnimation);rootView.postDelayed(new Runnable() {@Overridepublic void run() {TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT);translateAnimation1.setDuration(1500);translateAnimation1.setFillAfter(true);rootView.startAnimation(translateAnimation1);}},3000);}
}

android DecorView的使用就讲完了。

就这么简单。

android DecorView的使用相关推荐

  1. android decorview动画,Android窗口机制(二)Window,PhoneWindow,DecorView,setContentView源码理解...

    Android窗口机制系列 前篇文章中出现了PhoneWindow,DecorView这些类,如果是第一次见过的话,肯定会觉得陌生.这篇文章主要跟大家讲解Window,PhoneWindow,Deco ...

  2. 理解Android DecorView

    一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. 三. ...

  3. Android DecorView浅析

    不知道原出处,所以只能表明转载处了:http://blog.csdn.net/sunny2come/article/details/8899138,顺便增加了个RobotiumViewer的截图以方便 ...

  4. android decorView详解

    摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...

  5. Android虚拟键盘的高度计算

    系统本身没有提供获取虚拟键盘的方法,在网上查了一些资料,发现还存在一点问题,故此把改好的代码记录下来,以备后用. 需要用OnGlobalLayoutListener来监听app窗口的变化 final ...

  6. Android Activity 生命周期详解及监听

    前言 系列文章: Android Activity 与View 的互动思考 Android Activity 生命周期详解及监听 Android onSaveInstanceState/onResto ...

  7. Kotlin Flow啊,你将流向何方?

    前言 协程系列文章: 一个小故事讲明白进程.线程.Kotlin 协程到底啥关系? 少年,你可知 Kotlin 协程最初的样子? 讲真,Kotlin 协程的挂起/恢复没那么神秘(故事篇) 讲真,Kotl ...

  8. 来,跟我一起撸Kotlin runBlocking/launch/join/async/delay 原理使用

    前言 协程系列文章: 一个小故事讲明白进程.线程.Kotlin 协程到底啥关系? 少年,你可知 Kotlin 协程最初的样子? 讲真,Kotlin 协程的挂起/恢复没那么神秘(故事篇) 讲真,Kotl ...

  9. Kotlin 协程调度切换线程是时候解开真相了

    前言 协程系列文章: 一个小故事讲明白进程.线程.Kotlin 协程到底啥关系? 少年,你可知 Kotlin 协程最初的样子? 讲真,Kotlin 协程的挂起/恢复没那么神秘(故事篇) 讲真,Kotl ...

最新文章

  1. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月25日-7月1日)
  2. HBase–常用API操作篇
  3. Linux上的free命令详解
  4. Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源
  5. shell实现简单的进程监控脚本
  6. TIMESTAMP和DATETIME哪个好
  7. Eigen(5)Array类和元素级操作
  8. 有关编程的12个猜想
  9. java报错空指针异常_分析使用Spring Boot进行单元测试时,报出空指针异常
  10. Hadoop大数据之Debug
  11. html______1
  12. jdk api官方文档的使用
  13. linux能运行关关采集器吗,杰奇linux采集器,基本能用且速度还行
  14. 从10万到百亿营收的背后 | 同程旅游CTO V课堂实录
  15. leetcode LCP 03. 机器人大冒险
  16. fullCalendar获取某一天的日程(event)
  17. R 语言中1 和1L的区别
  18. Python-各种Loss总结
  19. 笔记本电脑键盘失灵拯救方法总结
  20. sqlserver数据库全量备份

热门文章

  1. ActiveMQ简介与安装
  2. jQuery选择器中的特殊符号和关键字
  3. Linux(9.21-9.27)学习笔记
  4. SQL Server blocking session
  5. 如何使用Ubuntu打电话
  6. 设计模式读书笔记-----代理模式
  7. thinkingback no5
  8. CPU : Intel CPU命名规则
  9. 大二暑假周进度总结07
  10. UML学生成绩管理系统需求分析