{"id":773,"date":"2015-06-11T10:29:40","date_gmt":"2015-06-11T02:29:40","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=773"},"modified":"2026-08-18T12:58:41","modified_gmt":"2026-08-18T04:58:41","slug":"android%e7%95%8c%e9%9d%a2%e6%98%be%e7%a4%ba%e4%b8%8elayoutinflater","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=773","title":{"rendered":"Android\u754c\u9762\u663e\u793a\u4e0eLayoutInflater"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>LayoutInflater<\/strong> \u4e3b\u8981\u7528\u6765\u52a0\u8f7d\u5e03\u5c40\u3002\u521a\u63a5\u89e6 Android \u65f6\u53ef\u80fd\u5bf9\u5b83\u4e0d\u719f\uff0c\u56e0\u4e3a\u52a0\u8f7d\u5e03\u5c40\u901a\u5e38\u5728 Activity \u91cc\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">setContentView()<\/code> \u5c31\u5b8c\u6210\u4e86\u3002\u5176\u5b9e\u8fd9\u4e2a\u65b9\u6cd5\u5185\u90e8\u4e5f\u662f\u7528 LayoutInflater \u628a XML \u53d8\u6210 View \u6811\u3002<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e00\u3001inflate \u7684\u4e24\u79cd\u5e38\u89c1\u5199\u6cd5<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e00\u6b65\uff0c\u62ff\u5230 Inflater\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">LayoutInflater mInflater = LayoutInflater.from(context);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7b2c\u4e8c\u6b65\uff0c\u52a0\u8f7d\u5e03\u5c40\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">ViewGroup parent;\nView v = mInflater.inflate(R.layout.item_simpleadapter_view, parent, true);\nView v = mInflater.inflate(R.layout.item_simpleadapter_view, parent, false);\nView v = mInflater.inflate(R.layout.item_simpleadapter_view, null);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7b7e\u540d\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">public View inflate(int resourceId, ViewGroup root, boolean attachToRoot)<\/code><\/p>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all;\" width=\"817\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u53c2\u6570\u7ec4\u5408<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u884c\u4e3a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">root \u975e\u7a7a\uff0cattachToRoot = false<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u4ece root \u5f97\u5230\u7531 layout_width \/ layout_height \u7ec4\u6210\u7684 LayoutParams\uff0c\u8d4b\u7ed9\u52a0\u8f7d\u51fa\u7684 View\uff0c\u4f46\u4e0d\u6302\u5230 parent<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">root \u975e\u7a7a\uff0cattachToRoot = true<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u628a\u52a0\u8f7d\u7684\u89c6\u56fe\u4f5c\u4e3a\u5b50\u89c6\u56fe\u6dfb\u52a0\u5230 root<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">root = null<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u76f4\u63a5\u8fd4\u56de\u521b\u5efa\u7684\u89c6\u56fe<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e8c\u3001Activity \u7a97\u53e3\u7531\u54ea\u4e24\u90e8\u5206\u7ec4\u6210<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u4efb\u4f55\u4e00\u4e2a Activity \u91cc\u663e\u793a\u7684\u754c\u9762\uff0c\u5176\u5b9e\u4e3b\u8981\u90fd\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<strong>\u6807\u9898\u680f<\/strong>\u548c<strong>\u5185\u5bb9\u5e03\u5c40<\/strong>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6807\u9898\u680f\u662f\u5f88\u591a\u754c\u9762\u9876\u90e8\u90a3\u4e00\u6761\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u91cc\u63a7\u5236\u663e\u9690\u3002\u5185\u5bb9\u5e03\u5c40\u662f\u4e00\u4e2a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">FrameLayout<\/code>\uff0c\u8fd9\u4e2a\u5e03\u5c40\u7684 id \u53eb <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">content<\/code>\u3002\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">setContentView()<\/code> \u4f20\u5165\u7684\u5e03\u5c40\uff0c\u5176\u5b9e\u662f\u653e\u8fdb\u8fd9\u4e2a FrameLayout \u91cc\u2014\u2014\u6240\u4ee5\u65b9\u6cd5\u540d\u53eb setContentView\uff0c\u800c\u4e0d\u662f setView\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Activity \u7a97\u53e3\u7ec4\u6210\u5982\u4e0b\uff0c\u4fbf\u4e8e\u5bf9\u7167\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/06\/20131218231254906.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-774\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/06\/20131218231254906.png\" alt=\"20131218231254906\" width=\"537\" height=\"438\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/06\/20131218231254906.png 552w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/06\/20131218231254906-528x430.png 528w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/a><\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1asetContentView \u53ea\u662f LayoutInflater \u7684\u5feb\u6377\u65b9\u5f0f\uff0c\u771f\u6b63\u628a XML \u53d8\u6210 View\uff0c\u5e76\u585e\u8fdb\u7a97\u53e3\u91cc\u90a3\u4e2a id \u4e3a content \u7684 FrameLayout\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>LayoutInflater \u4e3b\u8981\u7528\u6765\u52a0\u8f7d\u5e03\u5c40\u3002\u521a\u63a5\u89e6 Android \u65f6\u53ef\u80fd\u5bf9\u5b83\u4e0d\u719f\uff0c\u56e0\u4e3a\u52a0\u8f7d\u5e03\u5c40\u901a\u5e38\u5728 A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[195],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/773"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=773"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}