{"id":4412,"date":"2021-07-28T10:58:23","date_gmt":"2021-07-28T02:58:23","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=4412"},"modified":"2021-07-28T15:54:26","modified_gmt":"2021-07-28T07:54:26","slug":"inflate%e5%8a%a0%e8%bd%bd%e7%9a%84%e5%b8%83%e5%b1%80match_parent%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%8d%e7%94%9f%e6%95%88","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412","title":{"rendered":"inflate\u52a0\u8f7d\u7684\u5e03\u5c40match_parent\u4e3a\u4ec0\u4e48\u4e0d\u751f\u6548"},"content":{"rendered":"<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/2430c1dd2722\">https:\/\/www.jianshu.com\/p\/2430c1dd2722<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#%E4%B8%80%E3%80%81LayoutInflater_inflate-%E5%88%9B%E5%BB%BAView\" title=\"\u4e00\u3001LayoutInflater inflate-\u521b\u5efaView\">\u4e00\u3001LayoutInflater inflate-\u521b\u5efaView<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#%E4%BA%8C%E3%80%81ViewGroup-%E6%B7%BB%E5%8A%A0View\" title=\"\u4e8c\u3001ViewGroup-\u6dfb\u52a0View\">\u4e8c\u3001ViewGroup-\u6dfb\u52a0View<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#%E7%8E%B0%E8%B1%A11%EF%BC%9A\" title=\"\u73b0\u8c611\uff1a\">\u73b0\u8c611\uff1a<\/a><ul class=\"ez-toc-list-level-5\"><li class=\"ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#Demo%E6%BA%90%E7%A0%81\" title=\"Demo\u6e90\u7801\">Demo\u6e90\u7801<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#%E5%8E%9F%E5%9B%A0\" title=\"\u539f\u56e0\">\u539f\u56e0<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#match_parent%E6%97%A0%E6%95%88%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95\" title=\"match_parent\u65e0\u6548\u7684\u89e3\u51b3\u529e\u6cd5\">match_parent\u65e0\u6548\u7684\u89e3\u51b3\u529e\u6cd5<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#%E7%8E%B0%E8%B1%A12%EF%BC%9A\" title=\"\u73b0\u8c612\uff1a\">\u73b0\u8c612\uff1a<\/a><ul class=\"ez-toc-list-level-5\"><li class=\"ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#Demo%E6%BA%90%E7%A0%81-2\" title=\"Demo\u6e90\u7801\">Demo\u6e90\u7801<\/a><ul class=\"ez-toc-list-level-6\"><li class=\"ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#1\" title=\"1)\">1)<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#2\" title=\"2)\">2)<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4412\/#3\" title=\"3)\">3)<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81LayoutInflater_inflate-%E5%88%9B%E5%BB%BAView\"><\/span>\u4e00\u3001LayoutInflater inflate-\u521b\u5efaView<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e0d\u540c\u7248\u672c\u7684LayoutInflater inflate View\u6e90\u7801\u53ef\u80fd\u4e0d\u5927\u4e00\u6837\uff0c\u5148\u5217\u4e3e\u6838\u5fc3\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">  View view = (View) inflater.invoke(null, mContext, resource);\n\n            if (view != null &amp;&amp; root != null) {\n                \/\/ We were able to use the precompiled inflater, but now we need to do some work to\n                \/\/ attach the view to the root correctly.\n                XmlResourceParser parser = res.getLayout(resource);\n                try {\n                    AttributeSet attrs = Xml.asAttributeSet(parser);\n                    advanceToRootNode(parser);\n                    ViewGroup.LayoutParams params = root.generateLayoutParams(attrs);\n\n                    if (attachToRoot) {\n                        root.addView(view, params);\n                    } else {\n                        view.setLayoutParams(params);\n                    }\n                } finally {\n                    parser.close();\n                }\n            }\n\nreturn view;\n<\/code><\/pre>\n<ul>\n<li>root\u4e3anull\uff0cview\u4e0d\u4f1a\u8bbe\u7f6eLayoutParams\uff0c\u4e5f\u4e0d\u4f1aadd\u5230root\u91cc\u3002<\/li>\n<li>root\u4e0d\u4e3anull<br \/>\n\u4f1a\u83b7\u53d6root\u521b\u5efa\u7684\u7684layoutParams<\/p>\n<p>attachToRoot\u4e3atrue\uff0c\u5c06view\u6dfb\u52a0\u5230root\u5e76\u8bbe\u7f6elayoutParams\u3002<br \/>\nattachToRoot\u4e3afalse\uff0c\u4e0d\u5c06view\u6dfb\u52a0\u5230root\uff0c\u4ec5\u8bbe\u7f6elayoutParams\u3002<\/p>\n<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81ViewGroup-%E6%B7%BB%E5%8A%A0View\"><\/span>\u4e8c\u3001ViewGroup-\u6dfb\u52a0View<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"%E7%8E%B0%E8%B1%A11%EF%BC%9A\"><\/span>\u73b0\u8c611\uff1a<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5bf9\u4e8e<\/p>\n<pre><code class=\"language-java line-numbers\">View child_view1 = LayoutInflater.from(this).inflate(R.layout.child_view1,null);\nviewPlacer.addView(child_view1);\n<\/code><\/pre>\n<p>\u7ed3\u679c\u4e3a:<\/p>\n<blockquote><p>\n  child_view1\u867d\u7136\u5728XML\u91cc\u5b9a\u4e49\u7684\u6839\u5e03\u5c40\u662fmatch_parent\uff0c\u4f46\u662f\u5b9e\u9645\u7ed9\u7528\u6237\u7684\u611f\u89c9\u662fwrap_conent\u3002\n<\/p><\/blockquote>\n<h5><span class=\"ez-toc-section\" id=\"Demo%E6%BA%90%E7%A0%81\"><\/span>Demo\u6e90\u7801<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>\u7531\u4e8e\u7cfb\u7edf\u81ea\u5e26\u7684\u7c7bLinearLayoutCompat\u65e0\u6cd5debug\u5b83\u7684addView\u65b9\u6cd5\uff0c\u8fd9\u91cc\u76f4\u63a5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u590d\u5199addView\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">public class MyLinearLayoutCompat extends LinearLayoutCompat {\n    public MyLinearLayoutCompat(@NonNull Context context) {\n        super(context);\n    }\n\n    public MyLinearLayoutCompat(@NonNull Context context, @Nullable  AttributeSet attrs) {\n        super(context, attrs);\n    }\n\n    public MyLinearLayoutCompat(@NonNull   Context context, @Nullable  AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n    }\n\n    @Override\n    public void addView(View child, int index) {\n        if (child == null) {\n            throw new IllegalArgumentException(\"Cannot add a null child view to a ViewGroup\");\n        }\n        ViewGroup.LayoutParams params = child.getLayoutParams();\n        if (params == null) {\n            params = generateDefaultLayoutParams();\n            if (params == null) {\n                throw new IllegalArgumentException(\n                        \"generateDefaultLayoutParams() cannot return null\");\n            }\n        }\n        addView(child, index, params);\n    }\n\n}\n<\/code><\/pre>\n<p>child_view1\u7684\u5e03\u5c40\u4e3a\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:background=\"#ff0000\"\n    &gt;\n    &lt;Button\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u6d4b\u8bd5\"\n        android:layout_centerInParent=\"true\"\n        \/&gt;\n&lt;\/RelativeLayout&gt;\n<\/code><\/pre>\n<h5><span class=\"ez-toc-section\" id=\"%E5%8E%9F%E5%9B%A0\"><\/span>\u539f\u56e0<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>match_parent\u53d8\u6210wrap_content\u7684\u539f\u56e0\u662fLayoutInflater\u5728\u521b\u5efaView\u7684\u65f6\u5019root\u4e3anull\uff0c\u6240\u4ee5\u521b\u5efa\u7684View\u7684layoutParams\u4e3anull\uff0c\u5728ViewGroup addView\u7684\u65f6\u5019\u5c31\u4f1a\u8c03\u7528<br \/>\ngenerateDefaultLayoutParams()\u65b9\u6cd5\u751f\u6210layoutParams\u6765addView\uff0c\u800c\u5bf9\u4e8egenerateDefaultLayoutParams\u4e0d\u540c\u7684\u5e03\u5c40\u7684\u5b9e\u73b0\u4e00\u822c\u90fd\u4e0d\u4e00\u6837\uff0c\u6bd4\u5982\u7ebf\u6027\u5e03\u5c40\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\"> @Override\n    protected LayoutParams generateDefaultLayoutParams() {\n        if (mOrientation == HORIZONTAL) {\n            return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);\n        } else if (mOrientation == VERTICAL) {\n            return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);\n        }\n        return null;\n    }\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u6a2a\u5411\u7684\u7ebf\u6027\u5e03\u5c40\uff0c\u4f60\u770b\u5230\u7684\u6548\u679c\u5c31\u662fwrap_wrap;\u5bf9\u4e8e\u7ad6\u5411\u7684\u7ebf\u6027\u5e03\u5c40\uff0c\u4f60\u770b\u5230\u7684\u6548\u679c\u5c31\u662fmatch_wrap\u3002<\/p>\n<h5><span class=\"ez-toc-section\" id=\"match_parent%E6%97%A0%E6%95%88%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95\"><\/span>match_parent\u65e0\u6548\u7684\u89e3\u51b3\u529e\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>1.inflate View\u7684\u65f6\u5019\u6307\u5b9aroot\u5e76\u4e14attachToParent\u3002<\/li>\n<li>2.inflate View\u7684\u65f6\u5019\u4e0d\u6307\u5b9aroot\uff0caddView\u7684\u65f6\u5019\u6dfb\u52a0match_parent\u7684LayoutParams\u3002<\/li>\n<li>3.inflate View\u7684\u65f6\u5019\u4e0d\u6307\u5b9aroot\uff0caddView\u7684\u65f6\u5019\u4e5f\u4e0d\u6dfb\u52a0match_parent\u7684LayoutParams\uff0c\u6539\u5e03\u5c40\u5373\u53ef\uff1a\u5728\u5e03\u5c40\u7684\u6839\u5e03\u5c40\u4e0b\u9762\u518d\u5305\u4e00\u5c42match_parent\u3002<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"%E7%8E%B0%E8%B1%A12%EF%BC%9A\"><\/span>\u73b0\u8c612\uff1a<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4e3a\u4ec0\u4e48\u201c\u73b0\u8c611\u201d-\u201cmatch_parent\u65e0\u6548\u7684\u89e3\u51b3\u529e\u6cd5\u201d\u7684\u7b2c3\u79cd\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u5462\uff1f<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Demo%E6%BA%90%E7%A0%81-2\"><\/span>Demo\u6e90\u7801<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>child_view2\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:background=\"#ff0000\"\n    &gt;\n    &lt;RelativeLayout  android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:background=\"#00ff00\"\n        &gt;\n    &lt;Button\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u6d4b\u8bd5\"\n        android:layout_centerInParent=\"true\"\n        \/&gt;\n    &lt;\/RelativeLayout&gt;\n&lt;\/RelativeLayout&gt;\n<\/code><\/pre>\n<p>\u611f\u89c9\u7cfb\u7edf\u5e94\u8be5\u662f\u5bf9\u5e03\u5c40\u7684\u6700\u5916\u5c42\u7684\u6839\u5e03\u5c40\u53ef\u80fd\u505a\u4e86\u4ec0\u4e48\u9a9a\u64cd\u4f5c\uff0c\u4e8e\u662f\u5c06\u6839\u5e03\u5c40\u7531match\u4fee\u6539\u4e3a\u56fa\u5b9a\u503c\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"300px\"\n    android:layout_height=\"200px\"\n    android:background=\"#ff0000\"\n    &gt;\n    &lt;RelativeLayout  android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:background=\"#00ff00\"\n        &gt;\n    &lt;Button\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u6d4b\u8bd5\"\n        android:layout_centerInParent=\"true\"\n        \/&gt;\n    &lt;\/RelativeLayout&gt;\n&lt;\/RelativeLayout&gt;\n<\/code><\/pre>\n<p>\u518d\u4f7f\u7528child_view2\u6211\u53c8\u505a\u4e86\u4e00\u7ec4\u5bf9\u6bd4\u64cd\u4f5c\uff1a<\/p>\n<h6><span class=\"ez-toc-section\" id=\"1\"><\/span>1)<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre><code class=\"language-java line-numbers\"> LayoutInflater.from(this).inflate(R.layout.child_view2,viewPlacer);\n<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\u6b63\u5e38\u7684\u56fa\u5b9a\u503c\u663e\u793a<\/p>\n<h6><span class=\"ez-toc-section\" id=\"2\"><\/span>2)<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre><code class=\"language-java line-numbers\">View child_view2 = LayoutInflater.from(this).inflate(R.layout.child_view2,null);\nviewPlacer.addView(child_view2);\n<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\u5f02\u5e38match_match\u663e\u793a\u3002<\/p>\n<h6><span class=\"ez-toc-section\" id=\"3\"><\/span>3)<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre><code class=\"language-java line-numbers\">View child_view2 = LayoutInflater.from(this).inflate(R.layout.child_view2,viewPlacer,false);\nviewPlacer.addView(child_view2);\n<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\u6b63\u5e38\u7684\u56fa\u5b9a\u503c\u663e\u793a<\/p>\n<p>\u6240\u4ee5\uff0c<strong>\u901a\u8fc7LayoutInflater\u76f4\u63a5inflate\u5e76\u4e14attachToParent\u586b\u5145View<\/strong>\u4e0e<strong>inflate\u751f\u6210View\u540e\u518daddView<\/strong>\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n<p>\u7b54\u6848\uff1a\u53c2\u8003\u6211\u7684\u53e6\u4e00\u7bc7\u6587\u7ae0<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/xinyiworld.top\/wordpress\/?p=4422\">http:\/\/xinyiworld.top\/wordpress\/?p=4422<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"4412\" 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>https:\/\/www.jianshu.com\/p\/2430c1dd2722 \u76ee\u5f55 \u4e00\u3001Layout [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[181],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4412"}],"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=4412"}],"version-history":[{"count":7,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4412\/revisions"}],"predecessor-version":[{"id":4428,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4412\/revisions\/4428"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4412"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}