Android onFinishInflate 的理解

Android onFinishInflate 的理解onFinishInflate是添加在layoutxml里面的view被LayoutInflater解析完addview之后在回调的onFinishInflate方法。构造函数是实例化一个View都会执行的方法。从执行顺序来看也确实是构造函数执行早于onFinishInflateonFinishInflate执行早于onMeasureonLayout函数publicclassCustomLinerLayoutextendsLinearLayout{…

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38413.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注