Android onFinishInflate 的理解

onFinishInflate  是添加在layout xml里面的view 被LayoutInflater 解析完addview 之后再回调的onFinishInflate 方法。
构造函数是实例化一个View 都会执行的方法。
从执行顺序来看也确实是构造函数执行早于 onFinishInflate
onFinishInflate 执行早于 onMeasure onLayout等函数

public class CustomLinerLayout extends LinearLayout {
    private static final String TAG = "CustomLinerLayout";

    public CustomLinerLayout(Context context) {
        super(context);
        Log.i(TAG,"CustomLinerLayout construction 1");
    }

    public CustomLinerLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG,"CustomLinerLayout construction 2");
    }

    public CustomLinerLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.i(TAG,"CustomLinerLayout construction 3");
    }

    public CustomLinerLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        Log.i(TAG,"CustomLinerLayout construction 4");
    }

    @Override
    protected void onFinishInflate() {
        Log.i(TAG,"onFinishInflate start");
        super.onFinishInflate();
        Log.i(TAG,"onFinishInflate end",new Exception());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG,"onMeasure");
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.i(TAG,"onLayout");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG,"onDraw");
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        Log.i(TAG,"onWindowVisibilityChanged " + visibility);
    }
}

 layout 文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.demo.view.CustomLinerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30dp"
        android:text="Test onFinishInflate and construction"
        />
</com.example.demo.view.CustomLinerLayout>

 打印log:

Line 290: 06-18 15:33:12.340 17720 17720 I CustomLinerLayout: CustomLinerLayout construction 2
	Line 291: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: onFinishInflate start
	Line 292: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: onFinishInflate end
	Line 293: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: java.lang.Exception
	Line 294: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at com.example.demo.view.CustomLinerLayout.onFinishInflate(CustomLinerLayout.java:38)
	Line 295: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:1136)
	Line 296: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
	Line 297: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
	Line 298: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
	Line 299: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
	Line 300: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:460)
	Line 301: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.Activity.setContentView(Activity.java:3501)
	Line 302: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at com.example.demo.view.TestViewActivity.onCreate(TestViewActivity.java:20)
	Line 303: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.Activity.performCreate(Activity.java:8011)
	Line 304: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.Activity.performCreate(Activity.java:7991)
	Line 305: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1330)
	Line 306: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3701)
	Line 307: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3885)
	Line 308: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:100)
	Line 309: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
	Line 310: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
	Line 311: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2258)
	Line 312: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.os.Handler.dispatchMessage(Handler.java:106)
	Line 313: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.os.Looper.loopOnce(Looper.java:201)
	Line 314: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.os.Looper.loop(Looper.java:288)
	Line 315: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at android.app.ActivityThread.main(ActivityThread.java:8031)
	Line 316: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at java.lang.reflect.Method.invoke(Native Method)
	Line 317: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:555)
	Line 318: 06-18 15:33:12.341 17720 17720 I CustomLinerLayout: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:990)
	Line 321: 06-18 15:33:12.353 17720 17720 I CustomLinerLayout: onWindowVisibilityChanged 0
	Line 322: 06-18 15:33:12.356 17720 17720 I CustomLinerLayout: onMeasure
	Line 333: 06-18 15:33:12.360 17720 17720 I CustomLinerLayout: onMeasure
	Line 334: 06-18 15:33:12.360 17720 17720 I CustomLinerLayout: onLayout
	Line 681: 06-18 15:33:17.737 17720 17720 I CustomLinerLayout: onWindowVisibilityChanged 8

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值