{"id":486,"date":"2015-04-20T19:50:03","date_gmt":"2015-04-20T11:50:03","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=486"},"modified":"2015-04-20T19:54:35","modified_gmt":"2015-04-20T11:54:35","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":"<p>\u4ee3\u7801\u5b9e\u73b0\u7ebf\u578b\u5e03\u5c40\u548c\u76f8\u5bf9\u5e03\u5c40\u4ee5\u5165LayoutInflater\u4f7f\u7528<br \/>\nLayoutInflater\u4ecb\u7ecd<br \/>\n \u00a0<!--more--><br \/>\n    [java] LayoutInflater mInflater\u00a0= LayoutInflater.from(context);<br \/>\n\u00a0\u00a0\u00a0\u00a0 ViewGroup parent;<br \/>\n\u00a0\u00a0\u00a0\u00a0 View v\u00a0= mInflater.inflate(R.layout.item_simpleadapter_view, parent,true);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0View v\u00a0= mInflater.inflate(R.layout.item_simpleadapter_view, parent,false);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0View v = mInflater.inflate(R.layout.item_simpleadapter_view, null);[\/java]<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 public View inflate(int\u00a0resourceId, ViewGroup root, boolean attachToRoot)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0  <1>\u5982\u679c\u8bbe\u7f6e\u4e86ViewGroup root\u53c2\u6570\uff0c\u4e14attachToRoot\u8bbe\u7f6e\u4e3afalse\u7684\u8bdd,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0  \u5219\u4f1a\u4eceroot\u4e2d\u5f97\u5230\u7531layout_width\u548clayout_height\u7ec4\u6210\u7684LayoutParams,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0  \u5c31\u4f1a\u5bf9\u6211\u4eec\u52a0\u8f7d\u7684\u89c6\u56feView\u8bbe\u7f6e\u8be5LayoutParams\u3002<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0  <2>\u5982\u679c\u8bbe\u7f6e\u4e86ViewGroup root\u53c2\u6570\uff0c\u4e14attachToRoot\u8bbe\u7f6e\u4e3atrue\u7684\u8bdd\uff0c<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0  \u5219\u5c06\u6211\u4eec\u52a0\u8f7d\u7684\u89c6\u56fe\u505a\u4e3a\u5b50\u89c6\u56fe\u6dfb\u52a0\u5230root\u89c6\u56fe\u4e2d\u3002<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0 <3>\u5982\u679c\u6211\u4eecViewGroup root\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u8bdd\uff0c\u5c31\u76f4\u63a5\u8fd4\u56de\u6211\u4eec\u521b\u5efa\u7684\u89c6\u56fe\uff1b<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0<br \/>\n\u4ee3\u7801\u5b9e\u73b0\u7ebf\u578b\u5e03\u5c40\u548c\u76f8\u5bf9\u5e03\u5c40<\/p>\n<p>[java]public void setLineraLayout() {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0LinearLayout lineraLayout = newLinearLayout(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lineraLayout.setOrientation(LinearLayout.VERTICAL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0LayoutParams layoutParams = newLinearLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lineraLayout.setLayoutParams(layoutParams);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0TextView textView = newTextView(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView.setGravity(Gravity.CENTER);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView.setText(&quot;\u5b50\u5143\u7d200&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView.setTextSize(20);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView.setBackgroundColor(R.color.myblue);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView.setLayoutParams(newLinearLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0TextView textView1 = newTextView(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView1.setGravity(Gravity.CENTER);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView1.setText(&quot;\u5b50\u5143\u7d201&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView1.setTextSize(20);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView1.setBackgroundColor(Color.BLUE);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0textView1.setLayoutParams(newLinearLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0lineraLayout.addView(textView);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lineraLayout.addView(textView1);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0setContentView(lineraLayout);<\/p>\n<p>\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0publicvoidsetRelationLayout() {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0RelativeLayout rl = newRelativeLayout(this);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0Button btn1 = newButton(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn1.setText(&quot;&#8212;&#8212;&#8212;-1&#8212;&#8212;&#8212;&#8212;&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn1.setId(1);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0RelativeLayout.LayoutParams lp1 = newRelativeLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ btn1 \u4f4d\u4e8e\u7236 View \u7684\u9876\u90e8\uff0c\u5728\u7236 View \u4e2d\u6c34\u5e73\u5c45\u4e2d<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0rl.addView(btn1, lp1);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0Button btn2 = newButton(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn2.setText(&quot;|\\n|\\n2\\n|\\n|\\n|&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn2.setId(2);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0RelativeLayout.LayoutParams lp2 = newRelativeLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp2.addRule(RelativeLayout.BELOW, 1);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ btn2 \u4f4d\u4e8e btn1 \u7684\u4e0b\u65b9\u3001\u5176\u5de6\u8fb9\u548c btn1 \u7684\u5de6\u8fb9\u5bf9\u9f50<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0rl.addView(btn2, lp2);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0Button btn3 = newButton(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn3.setText(&quot;|\\n|\\n3\\n|\\n|\\n|&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn3.setId(3);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0RelativeLayout.LayoutParams lp3 = newRelativeLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp3.addRule(RelativeLayout.BELOW, 1);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp3.addRule(RelativeLayout.RIGHT_OF, 2);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ btn3 \u4f4d\u4e8e btn1 \u7684\u4e0b\u65b9\u3001btn2 \u7684\u53f3\u65b9\u4e14\u5176\u53f3\u8fb9\u548c btn1 \u7684\u53f3\u8fb9\u5bf9\u9f50\uff08\u8981\u6269\u5145\uff09<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0rl.addView(btn3, lp3);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0Button btn4 = newButton(this);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn4.setText(&quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;4&#8212;&#8212;&#8212;&#8212;&#8211;&quot;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0btn4.setId(4);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0RelativeLayout.LayoutParams lp4 = newRelativeLayout.LayoutParams(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ViewGroup.LayoutParams.WRAP_CONTENT);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp4.addRule(RelativeLayout.BELOW, 2);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ btn4 \u4f4d\u4e8e btn2 \u7684\u4e0b\u65b9\uff0c\u5728\u7236 Veiw \u4e2d\u6c34\u5e73\u5c45\u4e2d<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0rl.addView(btn4, lp4);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0setContentView(rl);<\/p>\n<p>\u00a0\u00a0}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u5b9e\u73b0\u7ebf\u578b\u5e03\u5c40\u548c\u76f8\u5bf9\u5e03\u5c40\u4ee5\u5165LayoutInflater\u4f7f\u7528 LayoutInflater\u4ecb\u7ecd \u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_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":7,"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}]}}