在重写View的时候,会遇到这两个方法

protected void onAttachedToWindow()

Description copied from class: View

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before View.onDraw(Android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after View.onMeasure(int, int).
Overrides:
onAttachedToWindow in class View

当此view附加到窗体上时调用该方法。在这时,view有了一个用于显示的Surface,将开始绘制。注意,此方法要保证在调用onDraw(Canvas)
之前调用,但可能在调用 onDraw(Canvas) 之前的任何时刻,包括调用 onMeasure(int, int)
之前或之后。

看得出次方法在onDraw方法之前调用,也就是view还没有画出来的时候,可以在此方法中去执行一些初始化的操作,google的AlarmClock动态时钟View就是在这个方法中进行广播的注册,代码如下:

  1. @Override
  2. protected void onAttachedToWindow() {
  3. super.onAttachedToWindow();
  4. if (Log.LOGV) Log.v("onAttachedToWindow " + this);
  5. if (mAttached) return;
  6. mAttached = true;
  7. if (mAnimate) {
  8. setBackgroundResource(R.drawable.animate_circle);
  9. /* Start the animation (looped playback by default). */
  10. ((AnimationDrawable) getBackground()).start();
  11. }
  12. if (mLive) {
  13. /* monitor time ticks, time changed, timezone */
  14. IntentFilter filter = new IntentFilter();
  15. filter.addAction(Intent.ACTION_TIME_TICK);
  16. filter.addAction(Intent.ACTION_TIME_CHANGED);
  17. filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
  18. mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
  19. }
  20. /* monitor 12/24-hour display preference */
  21. mFormatChangeObserver = new FormatChangeObserver();
  22. mContext.getContentResolver().registerContentObserver(
  23. Settings.System.CONTENT_URI, true, mFormatChangeObserver);
  24. updateTime();
  25. }

另外在屏蔽Home键的时候也会用到

  1. public void onAttachedToWindow() {
  2. this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
  3. super.onAttachedToWindow();
  4. }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

protected void onDetachedFromWindow()

Description copied from class: View
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
Overrides:
onDetachedFromWindow in class AdapterView<ListAdapter>

将视图从窗体上分离的时候调用该方法。这时视图已经不具有可绘制部分。

onDetachedFromWindow()正好与onAttachedToWindow()的用法相对应,在destroy view的时候调用,所以可以加入取消广播注册等的操作,还是google的闹钟代码:

  1. @Override
  2. protected void onDetachedFromWindow() {
  3. super.onDetachedFromWindow();
  4. if (!mAttached) return;
  5. mAttached = false;
  6. Drawable background = getBackground();
  7. if (background instanceof AnimationDrawable) {
  8. ((AnimationDrawable) background).stop();
  9. }
  10. if (mLive) {
  11. mContext.unregisterReceiver(mIntentReceiver);
  12. }
  13. mContext.getContentResolver().unregisterContentObserver(
  14. mFormatChangeObserver);
  15. }

具体的用法视个人的需求而定了,自己控制重写就好了。

Android重写view时onAttachedToWindow () 和 onDetachedFromWindow ()的更多相关文章

  1. android重写view和viewgroup的区别

    重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...

  2. Android 自定义View 三板斧之三——重写View来实现全新控件

    通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 本文来讨论最难的一种 ...

  3. Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题

    这里只做个汇总~.~独一无二 文章出处:http://blog.csdn.net/djy1992/article/details/9715047 Android自定义View研究--View中的原点坐 ...

  4. Android Studio 重写方法时参数命名异常

    Android Studio 重写方法时参数命名异常 Android Studio 重写方法时参数名称乱掉可以通过下载相应源码解决

  5. android 自定义view详解

    1.自定义View前首先要了解一下View的方法,虽然有些不一定要实现. 分类 方法 描述 创建 Constructors View中有两种类型的构造方法,一种是在代码中构建View,另一种是填充布局 ...

  6. onAttachedToWindow () 和 onDetachedFromWindow () ; 以及更新视图的函数ondraw() 和dispatchdraw()的区别

    protected void onAttachedToWindow() This is called when the view is attached to a window. At this po ...

  7. 每日一问:浅谈 onAttachedToWindow 和 onDetachedFromWindow

    基本上所有 Android 开发都会接触到 onCreate().onDestory().onStart().onStop() 等这些生命周期方法,但却不是所有人都会去关注到 onAttachXXX( ...

  8. Android之View的内容

    View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...

  9. Android之view的工作原理2

    学习内容 View的底层工作原理,比如View的测量流程.布局流程以及绘制流程:以及常见的View回调方法:熟悉掌握前面的知识后,自定义View的时候也会更加的得心应手. 4.1 初识ViewRoot ...

随机推荐

  1. HDU 3376 费用流 Matrix Again

    题意: 给出一个n × n的矩阵,每个格子中有一个数字代表权值,找出从左上角出发到右下角的两条不相交的路径(起点和终点除外),使得两条路径权值之和最大. 分析: 如果n比较小的话是可以DP的,但是现在 ...

  2. html-body相关标签

    一 字体标签   字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub& ...

  3. 使用spyder3调试python程序

    Ctrl+F5 以Debug模式运行文件 在debug之前记得用%reset 指令清空一下ipython工作空间中的变量,以免影响debug中变量值的查看 无论你是否打断点,都会在第一行语句执行之前中 ...

  4. SQL中,WHERE HAVING的区别

    WHERE是约束声明,是用来约束选数据库中储存的值的,其是在查询返回结果集之前起作用,其必须作用于数据库中存在的值,因此不能用聚合函数(avg,sum,count等,因为这些函数返回的值并非数据库中储 ...

  5. 简单实用jstl实现代码编写

    package com.ceshi; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.s ...

  6. fish shell安装和配置

    sudo apt-get install fish whereis fish chsh -s /usr/bin/fish 重启:

  7. jQUery中的$(document).ready()方法和window.onload()方法的区别

    1.常规的Javascript代码中,通常使用window.onload方法 window.onload = function(){//代码} 2.jquery中,则使用$(document).rea ...

  8. [转] Makefile 基础 (1) —— Makefile 介绍

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...

  9. java中static变量和方法的总结

    转自:http://blog.csdn.net/haobo920/article/details/5921621 java中static变量和方法的总结 java中一切皆是对象 一个类中对象的定义一般 ...

  10. APUE 学习笔记(四) 标准I/O库

    1.流与FILE对象 unix I/O系统调用都是针对文件描述符的 标准C的I/O函数都是针对流(文件指针)的,我们使用一个流与一个文件相关联   2.缓冲 标准I/O库提供缓冲的目的就是尽可能减少r ...