前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2014-11-8Android学习-------onFinishInflate方法-------动画Animation学习篇

2014-11-8Android学习-------onFinishInflate方法-------动画Animation学习篇

作者头像
wust小吴
发布2022-03-07 14:22:17
6550
发布2022-03-07 14:22:17
举报
文章被收录于专栏:风吹杨柳风吹杨柳

onFinishInflate

我们一般使用View的流程是在onCreate中使用setContentView来设置要显示Layout文件或直接创建一个View,

在当设置了ContentView之后系统会对这个View进行解析,然后回调当前视图View中的onFinishInflate方法。

只有解析了这个View我们才能在这个View容器中获取到拥有Id的组件,同样因为系统解析完View之后才会调用onFinishInflate方法,

所以我们自定义组件时可以onFinishInflate方法中获取指定子View的引用。

自定义View常处理的回调函数

onFinishInflate() 当View中所有的子控件均被映射成xml后触发

onMeasure(int, int) 确定所有子元素的大小

onLayout(boolean, int, int, int, int) 当View分配所有的子元素的大小和位置时触发

onSizeChanged(int, int, int, int) 当view的大小发生变化时触发

onDraw(Canvas) view渲染内容的细节

onKeyDown(int, KeyEvent) 有按键按下后触发

onKeyUp(int, KeyEvent) 有按键按下后弹起时触发

onTrackballEvent(MotionEvent) 轨迹球事件

onTouchEvent(MotionEvent) 触屏事件

onFocusChanged(boolean, int, Rect) 当View获取或失去焦点时触发

onWindowFocusChanged(boolean) 当窗口包含的view获取或失去焦点时触发

onAttachedToWindow() 当view被附着到一个窗口时触发

onDetachedFromWindow() 当view离开附着的窗口时触发,该方法和 onAttachedToWindow() 是相反。

onWindowVisibilityChanged(int) 当窗口中包含的可见的view发生变化时触发

这里只看onFinishInflate()方法。下面从网上看到一个简单的测试代码,贴在下面,我后面讲到的Android抽屉效果也会再一次说到这个函数,

public class HeaderBar extends LinearLayout{....}

构造函数:

代码语言:javascript
复制
   public HeaderBar(Context context, AttributeSet attrs) {
        this(context, attrs, R.style.headerTitleBarStyle);
    }

    public HeaderBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.header, this, true);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HeaderBar);

        mLeftButtonBg = a.getDrawable(R.styleable.HeaderBar_leftButtonBackground);
        if (mLeftButtonBg == null) {
            mLeftButtonBg = context.getResources().getDrawable(R.drawable.back_btn_selector);
        }

        mRightButtonBg = a.getDrawable(R.styleable.HeaderBar_rightButtonBackground);
        if (mRightButtonBg == null) {
            mRightButtonBg = context.getResources().getDrawable(R.drawable.refresh);
        }

        mTitleTextViewButtonBg = a.getDrawable(R.styleable.HeaderBar_titleTextViewBackground);
        mTitle = a.getText(R.styleable.HeaderBar_title);
        
        a.recycle();
    }

关于R.styleable.HeaderBar_.....的问题请我看我的专题文章有介绍这个知识点

http://blog.csdn.net/u014737138/article/details/40789899

重载onFinishInflate()

代码语言:javascript
复制
   @Override
    protected void onFinishInflate() {
        //super.onFinishInflate();
        this.mTitleTextView = (TextView) this.findViewById(R.id.tv_header_title);
        this.mLeftButton = (Button) this.findViewById(R.id.btn_header_left);
        this.mRightButton = (Button) this.findViewById(R.id.btn_header_right);
        this.mLeftButton.setBackgroundDrawable(this.mLeftButtonBg);
        this.mRightButton.setBackgroundDrawable(this.mRightButtonBg);

        if (this.mTitleTextViewButtonBg != null) {
            //titleTextViewButtonBg = context.getResources().getDrawable(R.drawable.refresh);
            this.mTitleTextView.setBackgroundDrawable(this.mTitleTextViewButtonBg);
        }

        if (this.mTitle != null) {
            this.mTitleTextView.setText(this.mTitle);
        }

    }

从这个函数中的代码我们很清楚的知道它们干的是啥:

就是一个作用:获取指定子View布局文件中组件的引用,也就是找到这个组件的ID

onFinishInflate 当View中所有的子控件均被映射成xml后触发

我们接下来就是怎么使用的问题?

在需要使用自定义控件的layout文件,以包名+控件名作为标签名

注意:如果需要用自己的属性,要加上自己的命名空间:xmlns:xl=http://schemas.android.com/apk/res/com.xxx.abc 规则是:http://schemas.android.com/apk/res/ + 包名

这个在前面的自定义控件的文章也有讲过,详细的请看我前面的文章

代码语言:javascript
复制
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"  
代码语言:javascript
复制
    xmlns:xl="http://schemas.android.com/apk/res/com.xxx.abc"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f9f9f9"
    android:orientation="vertical" >

    <com.xxx.abc.view.HeaderBar
        android:id="@+id/lan_video_title_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xunlei:title="@string/lan_video_title" 
        xl:rightButtonBackground="@drawable/lan_video_add_btn_selector">
    </com.xxx.abc.view.HeaderBar>

参考地址: http://www.cnblogs.com/vivid-stanley/archive/2012/05/25/2518500.html

了解了它的作用,接下来我们还要啰嗦一句,就是这个函数什么时候调用,能做什么事?

比如你 自定义一个view叫myView ,路径是,com.test.view.MyView,此view是继承LinearLayout,定义的布局文件是my_view.xml 里面内容是: <com.test.view.MyView> <xxxx /> </com.test.view.MyView> 当你在使用的时候,可以这样使用 MyView mv = (MyView)View.inflate (context,R.layout.my_view,null); 当加载完成xml后,就会执行那个方法。

执行这个方法我们获得的是什么呢?---------------------------- 获得对这个布局文件中的组件的引用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • onFinishInflate
  • 自定义View常处理的回调函数
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档