setContentView()与LayoutInflater.inflate()关系

在Activity中调用setContentView(R.layout.activity_main)方法,就会进入AppCompatActivity中的setContentView(@LayoutRes int layoutResID)

public void setContentView(@LayoutRes int layoutResID) {
        this.getDelegate().setContentView(layoutResID);
}

首先分析AppCompatActivity中的this.getDelegate()

@NonNull
    public AppCompatDelegate getDelegate() {
        if (this.mDelegate == null) {
            this.mDelegate = AppCompatDelegate.create(this, this);
        }
        return this.mDelegate;
    }

在getDelegate()方法中通过AppCompatDelegate.create(this, this);创建了AppCompatDelegate 的实例mDelegate,接下来看AppCompatDelegate.create(this, this)

    public static AppCompatDelegate create(Activity activity, 
                                                AppCompatCallback callback) {
        return new AppCompatDelegateImpl(activity, activity.getWindow(), callback);
    }

create方法返回的是AppCompatDelegateImpl对象,那Activity中的setContentView()方法最终会调用AppCompatDelegateImpl类中的setContentView()方法,如下所示:

    public void setContentView(int resId) {
        this.ensureSubDecor();
        ViewGroup contentParent = (ViewGroup)this.mSubDecor.findViewById(16908290);
        contentParent.removeAllViews();
        LayoutInflater.from(this.mContext).inflate(resId, contentParent);
        this.mOriginalWindowCallback.onContentChanged();
    }

到这里就调用了LayoutInflater.from(this.mContext).inflate(resId, contentParent);
通过inflate方法最终将R.layout.activity_main加入到contentParent上;
inflate(resId, contentParent)方法最终都会调用到 inflate(parser, root, root != null);
接下来看inflate方法:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
            ...
            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }
                ...

                if (TAG_MERGE.equals(name)) {
                ...
                } else {
                    // Temp is the root view that was found in the xml
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
                        ...
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }
                    ...

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }
                ...

            return result;
        }

通过对上面代码分析:

  1. final View temp = createViewFromTag(root, name, inflaterContext, attrs);
  2. 由于root != null; 拿到布局参数params = root.generateLayoutParams(attrs);
  3. root != null && attachToRoot 为true;执行如下代码,将R.layout.activity_main加入到了DecorView上。
    if (root != null && attachToRoot) {
          root.addView(temp, params);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现以下报错的原因,改怎么解决,修改哪里2023-06-06 22:04:47.620 3151-3151/com.example.notepad E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.notepad, PID: 3151 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad/com.example.notepad.MainActivity}: android.view.InflateException: Binary XML file line #32: addView(View, LayoutParams) is not supported in AdapterView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2954) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3089) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6737) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) Caused by: android.view.InflateException: Binary XML file line #32: addView(View, LayoutParams) is not supported in AdapterView Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView at android.widget.AdapterView.addView(AdapterView.java:503) at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420) at android.app.Activity.setContentView(Activity.java:2772) at com.example.notepad.MainActivity.onCreate(MainActivity.java:30) at android.app.Activity.performCreate(Activity.java:7144) at android.app.Activity.performCreate(Activity.java:7135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2934) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3089) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6737) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值