{"id":5039,"date":"2021-10-08T15:52:02","date_gmt":"2021-10-08T07:52:02","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=5039"},"modified":"2021-10-20T14:25:24","modified_gmt":"2021-10-20T06:25:24","slug":"%e6%8d%a2%e8%82%a4%e5%8e%9f%e7%90%86%e7%a0%94%e7%a9%b6","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=5039","title":{"rendered":"\u6362\u80a4\u539f\u7406\u7814\u7a76"},"content":{"rendered":"<p>\u53c2\u8003\u8d44\u6599\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/hxl517116279\/article\/details\/96581407\">https:\/\/blog.csdn.net\/hxl517116279\/article\/details\/96581407<\/a>\uff08\u6362\u80a4\u539f\u7406\uff09<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/u011418943\/article\/details\/106863899\">https:\/\/blog.csdn.net\/u011418943\/article\/details\/106863899<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/my_csdnboke\/article\/details\/104168864\">https:\/\/blog.csdn.net\/my_csdnboke\/article\/details\/104168864<\/a>(hook \u5e03\u5c40\u4f18\u5316)<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/8ca35e86d476\">https:\/\/www.jianshu.com\/p\/8ca35e86d476<\/a>(\u5e03\u5c40\u4f18\u5316)<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.cnblogs.com\/Free-Thinker\/p\/3573391.html\">https:\/\/www.cnblogs.com\/Free-Thinker\/p\/3573391.html<\/a> \uff08apk\u901a\u8baf\u6362\u80a4\uff09<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.sohu.com\/a\/278212830_739982\">https:\/\/www.sohu.com\/a\/278212830_739982<\/a> \uff08\u6362\u80a4\u539f\u7406\u89e3\u6790\uff09<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/147683628\">https:\/\/zhuanlan.zhihu.com\/p\/147683628<\/a> \uff08\u975e\u5e38\u5168\u7684\u6362\u80a4\u65b9\u6848\uff09<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u6362\u80a4\u6846\u67b6\u672c\u8d28\u662f\u8981\u89e3\u51b3\u4e24\u4e2a\u95ee\u9898\uff1a\u627e\u5230<strong>\u6362\u80a4\u63a7\u4ef6\u6362\u80a4\u5c5e\u6027<\/strong>\uff0c<strong>\u7ed1\u5b9a\u76ae\u80a4\u7ed9\u63a7\u4ef6<\/strong>\uff08\u7ed1\u5b9a\u76ae\u80a4\u8d44\u6e90\u63a7\u4ef6\u7684\u5c5e\u6027\uff09<br \/>\n- \u627e\u6362\u80a4\u63a7\u4ef6<br \/>\n\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a\u901a\u8fc7hook AppcompatActivity\u7684factory2<br \/>\n\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a\u904d\u5386android.R.id.content\u5e03\u5c40\uff0c\u627e\u5230\u6240\u6709\u7684\u63a7\u4ef6\u3002<\/p>\n<pre><code>\u6362\u80a4\u63a7\u4ef6\u5305\u62ecxml\u91cc\u5b9a\u4e49\u7684\u548c\u4ee3\u7801\u91cc\u52a8\u6001\u521b\u5efa\u7684\n\u6362\u80a4\u63a7\u4ef6\u5b58\u5728\u4e8eActivity\uff0c\u4e5f\u5b58\u5728\u4e8eDialog\u3002\n<\/code><\/pre>\n<ul>\n<li>\u7ed1\u5b9a\u5c5e\u6027<br \/>\n\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a\u6839\u636e\u5c5e\u6027\u6765\u7ed1\u5b9aView<br \/>\n\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a\u6839\u636eView\u6765\u7ed1\u5b9a\u5c5e\u6027<\/p>\n<p>\u5c5e\u6027\u5305\u62ec\u7cfb\u7edf\u81ea\u5e26\u63a7\u4ef6\u7684\u5c5e\u6027\uff0c\u4e5f\u5305\u62ec\u81ea\u5b9a\u4e49\u63a7\u4ef6\u7684\u5c5e\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u6e90\u7801\u662fAPI29\uff0c\u7f51\u7edc\u4e0a\u5f88\u591a\u535a\u5ba2\u90fd\u662f\u5f88\u65e7\u7684\u6e90\u7801\uff0c\u6240\u4ee5\u7a0d\u5fae\u6709\u4e9b\u51fa\u5165\u3002<\/p>\n<h4>\u4e00\u3001View\u662f\u5982\u4f55\u521b\u5efa\u7684<\/h4>\n<p>AppCompatActivity->setContentView<br \/>\n\u2193<br \/>\nAppCompatDelegateImpl->setContentView<br \/>\n\u2193<br \/>\nLayoutInflater->inflate->createViewFromTag<br \/>\n->tryCreateView<\/p>\n<pre><code class=\"language-java line-numbers\"> View view;\n        if (mFactory2 != null) {\n            view = mFactory2.onCreateView(parent, name, context, attrs);\n        } else if (mFactory != null) {\n            view = mFactory.onCreateView(name, context, attrs);\n        } else {\n            view = null;\n        }\n<\/code><\/pre>\n<p>\u7531\u4e0a\u9762\u7684\u5206\u6790\u53ef\u4ee5\u77e5\u9053view\u662f\u7531mFactory2\u6216\u8005mFactory\u521b\u5efa\u7684\uff0c\u81f3\u4e8e\u662f\u54ea\u4e00\u4e2a\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b<br \/>\nAppCompatActivity->onCreate<\/p>\n<pre><code class=\"language-java line-numbers\"> @Override\n    protected void onCreate(@Nullable Bundle savedInstanceState) {\n        final AppCompatDelegate delegate = getDelegate();\n        delegate.installViewFactory();\n        delegate.onCreate(savedInstanceState);\n        super.onCreate(savedInstanceState);\n    }\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684delegate\u8ddf\u8e2a\u6e90\u7801\u53ef\u77e5\u662fAppCompatDelegateImpl\u7c7b<\/p>\n<pre><code class=\"language-java line-numbers\">@Override\n    public void installViewFactory() {\n        LayoutInflater layoutInflater = LayoutInflater.from(mContext);\n        if (layoutInflater.getFactory() == null) {\n            LayoutInflaterCompat.setFactory2(layoutInflater, this);\n        } else {\n            if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) {\n                Log.i(TAG, \"The Activity's LayoutInflater already has a Factory installed\"\n                        + \" so we can not install AppCompat's\");\n            }\n        }\n    }\n<\/code><\/pre>\n<p>\u6253\u65ad\u70b9\uff0c\u53ef\u77e5\u4e0a\u9762\u7684\u4ee3\u7801\u9ed8\u8ba4\u4f1a\u6267\u884c<br \/>\n<code>LayoutInflaterCompat.setFactory2(layoutInflater, this);<\/code><\/p>\n<p><strong>\u800c\u6700\u7ec8AppCompatDelegateImpl\u4f1a\u8c03\u7528AppCompatViewInflater\u521b\u5efaView<\/strong>\uff0cAppCompatViewInflater\u521b\u5efaView\u6709\u51e0\u79cd\u65b9\u5f0f\uff1a<br \/>\n1\uff09\u57fa\u7840\u63a7\u4ef6\u76f4\u63a5new AppCompatXXXView<br \/>\n2\uff09createViewFromTag<br \/>\n\u4e00\u79cd\u60c5\u51b5\u662f\u901a\u8fc7<code>&lt;view&gt;<\/code>\u58f0\u660e\u7684\u63a7\u4ef6\uff0c\u901a\u8fc7\u83b7\u53d6class\u5c5e\u6027\u6765\u83b7\u53d6\u5168\u7c7b\u540d;\u53e6\u5916\u662f\u7cfb\u7edf\u7279\u6b8a\u7684\u63a7\u4ef6\u6216\u8005\u81ea\u5b9a\u4e49\u63a7\u4ef6\u3002<\/p>\n<p>\u5982\u679c\u6807\u7b7e\u6ca1\u6709.\u53f7\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u4e0b\u9762\u4e09\u4e2a\u524d\u7f00\u5c1d\u8bd5\u53bb\u62fc\u63a5\u521b\u5efaView<\/p>\n<pre><code class=\"language-java line-numbers\">  \"android.widget.\",\n            \"android.view.\",\n            \"android.webkit.\"\n<\/code><\/pre>\n<p>\u5982\u679c\u6709\u5168\u7c7b\u540d\uff0c\u7cfb\u7edf\u4f1a\u76f4\u63a5\u53bb\u521b\u5efaView.<br \/>\n\u521b\u5efaView\u90fd\u662f\u901a\u8fc7\u53cd\u5c04\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">private View createViewByPrefix(Context context, String name, String prefix)\n            throws ClassNotFoundException, InflateException {\n        Constructor&lt;? extends View&gt; constructor = sConstructorMap.get(name);\n\n        try {\n            if (constructor == null) {\n                \/\/ Class not found in the cache, see if it's real, and try to add it\n                Class&lt;? extends View&gt; clazz = Class.forName(\n                        prefix != null ? (prefix + name) : name,\n                        false,\n                        context.getClassLoader()).asSubclass(View.class);\n\n                constructor = clazz.getConstructor(sConstructorSignature);\n                sConstructorMap.put(name, constructor);\n            }\n            constructor.setAccessible(true);\n            return constructor.newInstance(mConstructorArgs);\n        } catch (Exception e) {\n            \/\/ We do not want to catch these, lets return null and let the actual LayoutInflater\n            \/\/ try\n            return null;\n        }\n    }\n<\/code><\/pre>\n<p><strong>\u5173\u4e8eAppCompatXXXView<\/strong>\uff1a<br \/>\n\u5206\u6790\u6e90\u7801\u53ef\u4ee5\u77e5\u9053\uff0cAppCompatXXXView\u7684\u5404\u79cd\u5c5e\u6027\u7684\u7ed1\u5b9a\uff0c\u90fd\u662f\u901a\u8fc7\u5c5e\u6027\u5bf9\u5e94\u7684\u4e00\u4e2aHelper\u7c7b\u53bb\u5b9e\u73b0\u7684\u3002<\/p>\n<h4>\u4e8c\u3001\u81ea\u5b9a\u4e49\u521b\u5efaView\u7684Factory2\uff08Hook View\u7684\u521b\u5efa\uff09<\/h4>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\u53ef\u77e5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684Factory2\u3002<br \/>\n1\uff09\u5728AppCompatActivity\u6267\u884conCreate\u4e4b\u524d\uff0c\u8c03\u7528<code>LayoutInflaterCompat.setFactory2(layoutInflater, myFactory2);<\/code>\u8bbe\u7f6e\u81ea\u5df1\u7684Factory\u3002<br \/>\n2\uff09\u590d\u5199AppCompatActivity\u7684getDelegate\u65b9\u6cd5\uff0c\u81ea\u5b9a\u4e49AppCompatDelegate\u7684\u5b9e\u73b0\u7c7b\u3002<br \/>\n\u4e3a\u4ec0\u4e48\u8981\u590d\u5199----\u8ba9installFactory\u65b9\u6cd5\u4e3a\u7a7a\u5b9e\u73b0\u3002<\/p>\n<blockquote><p>\n  \u8bbe\u7f6e\u4e86Factory2\uff0c\u8bbe\u7f6e\u7684\u6700\u7ec8\u5bf9\u8c61\u662fcontext\u5bf9\u5e94\u7684inflater\uff0c\u6240\u4ee5\u5982\u679cactivity\u4e0a\u521b\u5efaDialog\uff0cdialog\u4f7f\u7528activity\u7684inflater\u53bb\u6362\u80a4\uff0c\u4e5f\u4f1a\u6709\u6362\u80a4\u7684\u6548\u679c\u3002\n<\/p><\/blockquote>\n<button class=\"simplefavorite-button\" data-postid=\"5039\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u8d44\u6599\uff1a https:\/\/blog.csdn.net\/hxl517116279\/article\/d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[269],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5039"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5039"}],"version-history":[{"count":21,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5039\/revisions"}],"predecessor-version":[{"id":5151,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5039\/revisions\/5151"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5039"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}