今天,简单讲讲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() {      @Override      public 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;//px   public 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() {      @Override      public void run() {        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT);        translateAnimation1.setDuration(1500);        translateAnimation1.setFillAfter(true);        rootView.startAnimation(translateAnimation1);      }    },3000);   }}


android DecorView的使用就讲完了。


就这么简单。

更多相关文章

  1. Android中获取应用程序(包)的信息-----PackageManager的使用(
  2. Android前置摄像头预览并检测人脸,获取人脸区域亮度
  3. android应用 小试牛刀 开发自己的应用程序就是这么简单
  4. 获取Android设备的方向
  5. Android:系统信息(内存、cpu、sd卡、电量、版本)的获取
  6. Android获取点击屏幕的位置坐标
  7. 学习笔记(01):FFmpeg打造Android万能音频播放器-OpenSL ES介绍并
  8. Android获取用户已安装app列表
  9. Android辅助功能(无障碍)使用---AccessibilityService

随机推荐

  1. android ContentProvider 使用实例
  2. How property system works on Android
  3. Android推送 基于MQTT
  4. Java/Android汉字转拼音
  5. Android(安卓)OkHeaderInterceptor
  6. android中 spannable的使用【转】
  7. android sdk manager’s error’s solves
  8. android view 上下左右滑动 事件
  9. Android之截屏代码
  10. Android基础入门教程——7.3.3 Android(