{"id":486,"date":"2015-04-20T19:50:03","date_gmt":"2015-04-20T11:50:03","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=486"},"modified":"2026-08-18T12:58:40","modified_gmt":"2026-08-18T04:58:40","slug":"android%e5%b8%83%e5%b1%80%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=486","title":{"rendered":"Android\u5e03\u5c40\u4ee3\u7801\u5b9e\u73b0"},"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;\">\u5e03\u5c40\u4e0d\u4e00\u5b9a\u53ea\u80fd\u5199 XML\u3002\u7528 <strong>LayoutInflater<\/strong> \u628a XML \u704c\u6210 View\uff0c\u6216\u7eaf\u4ee3\u7801 new \u51fa <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;\">LinearLayout<\/code> \/ <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;\">RelativeLayout<\/code>\uff0c\u90fd\u80fd\u5728\u8fd0\u884c\u65f6\u62fc\u754c\u9762\u3002\u5173\u952e\u662f <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;\">inflate(resource, root, attachToRoot)<\/code> \u4e09\u4e2a\u53c2\u6570\u600e\u4e48\u914d\u5408\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\u3001LayoutInflater \u4e09\u53c2\u6570<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u5148\u62ff\u5230 Inflater\uff0c\u518d inflate\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);\nViewGroup 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;\">\u5bf9\u5e94\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;\">root<\/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;\">attachToRoot<\/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;\">\u7ed3\u679c<\/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;\">\u975e\u7a7a<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">false<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u4ece root \u53d6\u51fa layout_width \/ layout_height \u7ec4\u6210 LayoutParams\uff0c\u8d4b\u7ed9\u52a0\u8f7d\u51fa\u6765\u7684 View\uff0c\u4f46\u4e0d addView<\/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;\">\u975e\u7a7a<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">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 View \u52a0\u8fdb 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;\">null<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\uff08\u5ffd\u7565\uff09<\/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\uff0c\u6ca1\u6709\u7236\u5e03\u5c40\u7684 LayoutParams<\/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\u3001\u4ee3\u7801\u5b9e\u73b0\u7ebf\u6027\u5e03\u5c40<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">new \u4e00\u4e2a\u5782\u76f4 LinearLayout\uff0c\u94fa\u6ee1\u7236\u5e03\u5c40\uff0c\u518d add \u4e24\u4e2a TextView\uff0c\u6700\u540e <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>\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;\">public void setLinearLayout() {\n  LinearLayout linearLayout = new LinearLayout(this);\n  linearLayout.setOrientation(LinearLayout.VERTICAL);\n\n  LayoutParams layoutParams = new LinearLayout.LayoutParams(\n      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);\n  linearLayout.setLayoutParams(layoutParams);\n\n  TextView textView = new TextView(this);\n  textView.setGravity(Gravity.CENTER);\n  textView.setText(\"\u5b50\u5143\u7d200\");\n  textView.setTextSize(20);\n  textView.setBackgroundColor(R.color.myblue);\n  textView.setLayoutParams(new LinearLayout.LayoutParams(\n      LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));\n\n  TextView textView1 = new TextView(this);\n  textView1.setGravity(Gravity.CENTER);\n  textView1.setText(\"\u5b50\u5143\u7d201\");\n  textView1.setTextSize(20);\n  textView1.setBackgroundColor(Color.BLUE);\n  textView1.setLayoutParams(new LinearLayout.LayoutParams(\n      LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));\n\n  linearLayout.addView(textView);\n  linearLayout.addView(textView1);\n  setContentView(linearLayout);\n}<\/code><\/pre>\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;\">\u4e09\u3001\u4ee3\u7801\u5b9e\u73b0\u76f8\u5bf9\u5e03\u5c40<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">RelativeLayout \u9760 <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;\">addRule<\/code> \u63cf\u8ff0\u8c01\u5728\u8c01\u4e0a\u3001\u8c01\u5bf9\u9f50\u8c01\u3002\u4e0b\u9762\u56db\u4e2a\u6309\u94ae\uff1abtn1 \u9876\u680f\u6c34\u5e73\u5c45\u4e2d\uff1bbtn2 \u5728\u5176\u4e0b\u4e14\u5de6\u5bf9\u9f50\uff1bbtn3 \u5728 btn1 \u4e0b\u65b9\u3001btn2 \u53f3\u4fa7\u5e76\u53f3\u5bf9\u9f50 btn1\uff1bbtn4 \u5728 btn2 \u4e0b\u65b9\u6c34\u5e73\u5c45\u4e2d\u3002<\/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;\">public void setRelationLayout() {\n  RelativeLayout rl = new RelativeLayout(this);\n\n  Button btn1 = new Button(this);\n  btn1.setText(\"----------1------------\");\n  btn1.setId(1);\n  RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(\n      ViewGroup.LayoutParams.WRAP_CONTENT,\n      ViewGroup.LayoutParams.WRAP_CONTENT);\n  lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);\n  lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);\n  \/\/ btn1 \u4f4d\u4e8e\u7236 View \u9876\u90e8\uff0c\u6c34\u5e73\u5c45\u4e2d\n  rl.addView(btn1, lp1);\n\n  Button btn2 = new Button(this);\n  btn2.setText(\"|\\n|\\n2\\n|\\n|\\n|\");\n  btn2.setId(2);\n  RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(\n      ViewGroup.LayoutParams.WRAP_CONTENT,\n      ViewGroup.LayoutParams.WRAP_CONTENT);\n  lp2.addRule(RelativeLayout.BELOW, 1);\n  lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);\n  \/\/ btn2 \u5728 btn1 \u4e0b\u65b9\uff0c\u5de6\u8fb9\u4e0e btn1 \u5bf9\u9f50\n  rl.addView(btn2, lp2);\n\n  Button btn3 = new Button(this);\n  btn3.setText(\"|\\n|\\n3\\n|\\n|\\n|\");\n  btn3.setId(3);\n  RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(\n      ViewGroup.LayoutParams.WRAP_CONTENT,\n      ViewGroup.LayoutParams.WRAP_CONTENT);\n  lp3.addRule(RelativeLayout.BELOW, 1);\n  lp3.addRule(RelativeLayout.RIGHT_OF, 2);\n  lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);\n  \/\/ btn3 \u5728 btn1 \u4e0b\u65b9\u3001btn2 \u53f3\u65b9\uff0c\u53f3\u8fb9\u4e0e btn1 \u5bf9\u9f50\uff08\u4f1a\u62c9\u5bbd\uff09\n  rl.addView(btn3, lp3);\n\n  Button btn4 = new Button(this);\n  btn4.setText(\"-----------------4--------------\");\n  btn4.setId(4);\n  RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(\n      ViewGroup.LayoutParams.WRAP_CONTENT,\n      ViewGroup.LayoutParams.WRAP_CONTENT);\n  lp4.addRule(RelativeLayout.BELOW, 2);\n  lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);\n  \/\/ btn4 \u5728 btn2 \u4e0b\u65b9\uff0c\u7236\u5e03\u5c40\u4e2d\u6c34\u5e73\u5c45\u4e2d\n  rl.addView(btn4, lp4);\n\n  setContentView(rl);\n}<\/code><\/pre>\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\uff1ainflate \u65f6 root + attachToRoot \u51b3\u5b9a\u300c\u53ea\u501f LayoutParams\u300d\u8fd8\u662f\u300c\u76f4\u63a5\u6302\u5230\u7236\u5e03\u5c40\u300d\uff1b\u7eaf\u4ee3\u7801\u5e03\u5c40\u5219\u662f new \u5bb9\u5668\u3001\u8bbe LayoutParams\u3001addView\uff0c\u6700\u540e setContentView\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5e03\u5c40\u4e0d\u4e00\u5b9a\u53ea\u80fd\u5199 XML\u3002\u7528 LayoutInflater \u628a XML \u704c\u6210 View\uff0c\u6216\u7eaf\u4ee3\u7801 new \u51fa  [&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\/486"}],"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=486"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":493,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/493"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}