{"id":1209,"date":"2016-02-07T21:25:55","date_gmt":"2016-02-07T13:25:55","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1209"},"modified":"2026-08-18T12:58:47","modified_gmt":"2026-08-18T04:58:47","slug":"android-view%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1209","title":{"rendered":"Android View\u603b\u7ed3"},"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;\">Android \u63a7\u4ef6\u5927\u81f4\u5206\u4e24\u7c7b\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;\">ViewGroup<\/code> \u5f53\u5bb9\u5668\u7ba1 View\uff0c<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;\">View<\/code> \u81ea\u5df1\u8d1f\u8d23\u7ed8\u5236\u3002\u6574\u68f5\u754c\u9762\u50cf DOM \u6811\uff1a\u7236\u89c6\u56fe\u8d1f\u8d23\u6d4b\u91cf\u3001\u5b9a\u4f4d\u3001\u7ed8\u5236\u3002<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;\">findViewById<\/code> \u4e4b\u6240\u4ee5\u8d35\uff0c\u662f\u56e0\u4e3a\u5b83\u4ece\u4e0a\u5230\u4e0b\u904d\u5386\u6574\u68f5\u63a7\u4ef6\u6811\u627e\u5b9e\u4f8b\uff0c\u91cd\u590d\u64cd\u4f5c\u91cc\u5c3d\u91cf\u5c11\u7528\u3002<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3498\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" width=\"2151\" height=\"1105\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png 2151w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-768x395.png 768w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-1536x789.png 1536w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-2048x1052.png 2048w\" sizes=\"(max-width: 2151px) 100vw, 2151px\" \/><\/a><\/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\u3001\u5173\u4e8e Android View \u63a7\u4ef6<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u73b0\u5728\u5e38\u7528\u63a7\u4ef6\u5927\u591a\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u81ea View\u3002ViewGroup \u4f5c\u4e3a\u5bb9\u5668\u7ba1\u7406\u5b50 View\uff0c\u7236\u89c6\u56fe\u8d1f\u8d23\u6d4b\u91cf\u3001\u5b9a\u4f4d\u548c\u7ed8\u5236\u3002<\/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;\">\u4e8c\u3001UI \u754c\u9762\u67b6\u6784<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u6bcf\u4e2a Activity \u542b\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;\">PhoneWindow<\/code>\uff0c\u5b83\u628a <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;\">DecorView<\/code> \u8bbe\u6210\u7a97\u53e3\u6839\u89c6\u56fe\u3002\u91cc\u9762\u662f\u719f\u6089\u7684 <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;\">TitleView<\/code> \u548c <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;\">ContentView<\/code>\u3002\u5e73\u65f6\u7684 <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> \u8bbe\u7684\u5c31\u662f ContentView\u3002<\/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;\">\u4e09\u3001Android \u5982\u4f55\u7ed8\u5236 View<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Activity \u542f\u52a8\u65f6\u4f1a\u88ab\u8981\u6c42\u753b\u51fa\u5e03\u5c40\u3002\u6846\u67b6\u5904\u7406\u8fd9\u4e2a\u8bf7\u6c42\u7684\u524d\u63d0\u662f\u5e03\u5c40\u5408\u7406\u3002\u7ed8\u5236\u4ece\u6839\u89c6\u56fe\u5f00\u59cb\uff0c\u4ece\u4e0a\u5230\u4e0b\u904d\u5386\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;\">ViewGroup<\/code> \u8d1f\u8d23\u8ba9\u5b50 View \u88ab\u7ed8\u5236\uff0c\u6bcf\u4e2a View \u901a\u8fc7 <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;\">draw()<\/code> \u753b\u81ea\u5df1\u3002\u4e09\u6b65\u8d70\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>Measure<\/section>\n<\/li>\n<li>\n<section>Layout<\/section>\n<\/li>\n<li>\n<section>Draw<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u6574\u6761\u6d41\u7a0b\u5728 <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;\">ViewRoot<\/code> \u7684 <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;\">performTraversals()<\/code> \u91cc\u5c55\u5f00\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;\">private void performTraversals() {\n    ......\n    \/\/\u6700\u5916\u5c42\u7684\u6839\u89c6\u56fe\u7684widthMeasureSpec\u548cheightMeasureSpec\u7531\u6765\n    \/\/lp.width\u548clp.height\u5728\u521b\u5efaViewGroup\u5b9e\u4f8b\u65f6\u7b49\u4e8eMATCH_PARENT\n    int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);\n    int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);\n    ......\n    mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);\n    ......\n    mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());\n    ......\n    mView.draw(canvas);\n    ......\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u753b\u4e4b\u524d\u5148\u77e5\u9053\u5c3a\u5bf8\u548c\u4f4d\u7f6e\uff0c\u6240\u4ee5\u5148 measure\u3001\u518d layout\u3002<\/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;\">\u56db\u3001Measure \u8fc7\u7a0b<\/span><\/h2>\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 final void measure(int widthMeasureSpec, int heightMeasureSpec) {\n    \/\/....\n    \/\/\u56de\u8c03onMeasure()\u65b9\u6cd5\n    onMeasure(widthMeasureSpec, heightMeasureSpec);\n    \/\/more\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8ba1\u7b97\u5b9e\u9645\u5927\u5c0f\uff0c\u5199\u5165 <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;\">mMeasuredHeight<\/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;\">mMeasuredWidth<\/code>\u3002<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;\">MeasureSpec<\/code> \u662f 32 \u4f4d int\uff1a\u9ad8 2 \u4f4d\u662f\u6a21\u5f0f\uff0c\u4f4e 30 \u4f4d\u662f\u5927\u5c0f\u3002\u4e09\u79cd\u6a21\u5f0f\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><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;\">EXACTLY<\/code>\uff1a\u7cbe\u786e\u503c\u3002layout_width\/height \u4e3a\u5177\u4f53\u6570\u503c\u6216 match_parent \u65f6\u4f7f\u7528<\/section>\n<\/li>\n<li>\n<section><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;\">AT_MOST<\/code>\uff1a\u6700\u5927\u503c\u3002wrap_content \u65f6\u4e0d\u80fd\u8d85\u8fc7\u7236\u63a7\u4ef6\u5141\u8bb8\u7684\u6700\u5927\u5c3a\u5bf8<\/section>\n<\/li>\n<li>\n<section><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;\">UNSPECIFIED<\/code>\uff1a\u4e0d\u6307\u5b9a\uff0cView \u60f3\u591a\u5927\u5c31\u591a\u5927\uff0c\u4e00\u822c\u5f88\u5c11\u7528<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">measure \u4e0d\u53ef\u91cd\u5199\uff0c\u81ea\u5b9a\u4e49\u65f6\u91cd\u5199 <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;\">onMeasure<\/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;\">protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {\n    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),\n            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6700\u7ec8\u5bbd\u9ad8\u7531 <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;\">setMeasuredDimension()<\/code> \u8bbe\u5b9a\uff1b\u4e0d\u91cd\u5199\u5219\u9ed8\u8ba4\u8d70 <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;\">getDefaultSize<\/code>\u3002<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;\">getMeasuredWidth()<\/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;\">getMeasuredHeight()<\/code> \u5fc5\u987b\u5728 onMeasure \u4e4b\u540e\u8c03\u7528\u624d\u6709\u6548\u3002<\/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;\">\u4e94\u3001Layout \u8fc7\u7a0b<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u77e5\u9053\u5927\u5c0f\u4e4b\u540e\u8fd8\u8981\u77e5\u9053\u4f4d\u7f6e\u624d\u80fd\u753b\u3002<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;\">mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());<\/code><\/p>\n<p style=\"margin: 1em 0px;\">\u56db\u4e2a\u53c2\u6570\u662f\u76f8\u5bf9\u7236\u89c6\u56fe\u7684\u5de6\u3001\u4e0a\u3001\u53f3\u3001\u4e0b\uff0c\u7528\u7684\u5c31\u662f\u521a\u6d4b\u51fa\u6765\u7684\u5bbd\u9ad8\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 layout(int l, int t, int r, int b) {\n    int oldL = mLeft;\n    int oldT = mTop;\n    int oldB = mBottom;\n    int oldR = mRight;\n    boolean changed = setFrame(l, t, r, b);\n    if (changed || (mPrivateFlags &amp; LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {\n        .....\n        onLayout(changed, l, t, r, b);\n        .....\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\"><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;\">setFrame<\/code> \u5199\u5165\u5750\u6807\u3002\u5750\u6807\u53d8\u4e86\u5c31\u91cd\u65b0\u5b9a\u4f4d\u3002View \u7684 onLayout \u662f\u7a7a\u65b9\u6cd5\uff0c\u5b9a\u4f4d\u7531 ViewGroup \u5b8c\u6210\u3002layout \u7ed3\u675f\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;\">getWidth()<\/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;\">getHeight()<\/code> \u624d\u8fd4\u56de\u6b63\u786e\u503c\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><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;\">getWidth\/Height()<\/code> \u548c <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;\">getMeasuredWidth\/Height()<\/code> \u7684\u533a\u522b\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><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;\">getWidth()<\/code>\uff1a\u5e03\u5c40\u8bbe\u5b9a\u5b8c\u6210\u540e\u6574\u4e2a View \u7684\u5bbd\u5ea6<\/section>\n<\/li>\n<li>\n<section><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;\">getMeasuredWidth()<\/code>\uff1a\u5bf9\u5185\u5bb9\u6d4b\u91cf\u540e\uff0c\u5185\u5bb9\u5360\u636e\u7684\u5bbd\u5ea6<\/section>\n<\/li>\n<\/ul>\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;\">API<\/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;\">\u4f55\u65f6\u6709\u6548<\/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;\">\u542b\u4e49<\/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;\">getMeasuredWidth\/Height<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">onMeasure \u4e4b\u540e<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6d4b\u91cf\u9636\u6bb5\u5f97\u5230\u7684\u5185\u5bb9\u5c3a\u5bf8<\/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;\">getWidth\/Height<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">layout \u4e4b\u540e<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5e03\u5c40\u5b8c\u6210\u540e View \u7684\u5b9e\u9645\u5bbd\u9ad8<\/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;\">\u516d\u3001Draw \u8fc7\u7a0b<\/span><\/h2>\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 draw(Canvas canvas) {\n        ......\n        \/*\n         * Draw traversal performs several drawing steps which must be executed\n         * in the appropriate order:\n         *\n         *      1. Draw the background\n         *      2. If necessary, save the canvas&#x27; layers to prepare for fading\n         *      3. Draw view&#x27;s content\n         *      4. Draw children\n         *      5. If necessary, draw the fading edges and restore layers\n         *      6. Draw decorations (scrollbars for instance)\n         *\/\n\n        \/\/ Step 1, draw the background, if needed\n        ......\n        if (!dirtyOpaque) {\n            drawBackground(canvas);\n        }\n\n        \/\/ skip step 2 &amp; 5 if possible (common case)\n        ......\n\n        \/\/ Step 2, save the canvas&#x27; layers\n        ......\n            if (drawTop) {\n                canvas.saveLayer(left, top, right, top + length, null, flags);\n            }\n        ......\n\n        \/\/ Step 3, draw the content\n        if (!dirtyOpaque) onDraw(canvas);\n\n        \/\/ Step 4, draw the children\n        dispatchDraw(canvas);\n\n        \/\/ Step 5, draw the fade effect and restore layers\n        ......\n        if (drawTop) {\n            matrix.setScale(1, fadeHeight * topFadeStrength);\n            matrix.postTranslate(left, top);\n            fade.setLocalMatrix(matrix);\n            p.setShader(fade);\n            canvas.drawRect(left, top, right, top + length, p);\n        }\n        ......\n\n        \/\/ Step 6, draw decorations (scrollbars)\n        onDrawScrollBars(canvas);\n        ......\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u91cd\u70b9\u662f\u7b2c 3 \u6b65 <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;\">onDraw<\/code>\u3002\u5176\u4f59\u662f\u80cc\u666f\u3001fade\u3001scrollbar\u3002<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;\">dispatchDraw<\/code> \u9012\u5f52\u753b\u5b50 View\uff1b\u6ca1\u6709\u5b50 View \u5c31\u4e0d\u9700\u8981\u3002onDraw \u8981\u81ea\u5df1\u5b9e\u73b0\uff0c\u6bcf\u4e2a\u63a7\u4ef6\u753b\u7684\u5185\u5bb9\u4e0d\u540c\uff0c\u4e3b\u8981\u7528 Canvas\u3002<\/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;\">\u4e03\u3001\u53c2\u8003\u8d44\u6599<\/span><\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><a href=\"http:\/\/blog.csdn.net\/guolin_blog\/article\/details\/16330267\" target=\"_blank\" rel=\"external noopener\">Android\u89c6\u56fe\u7ed8\u5236\u6d41\u7a0b\u5b8c\u5168\u89e3\u6790\uff0c\u5e26\u4f60\u4e00\u6b65\u6b65\u6df1\u5165\u4e86\u89e3View(\u4e8c)<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"http:\/\/blog.csdn.net\/yanbober\/article\/details\/46128379\" target=\"_blank\" rel=\"external noopener\">Android\u5e94\u7528\u5c42View\u7ed8\u5236\u6d41\u7a0b\u4e0e\u6e90\u7801\u5206\u6790<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"http:\/\/developer.android.com\/intl\/zh-cn\/guide\/topics\/ui\/how-android-draws.html\" target=\"_blank\" rel=\"external noopener\">How Android Draws Views<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"http:\/\/stackoverflow.com\/questions\/8657540\/what-is-the-difference-between-getwidth-height-and-getmeasuredwidth-height-i\" target=\"_blank\" rel=\"external noopener\">What is the difference between getWidth\/Height() and getMeasuredWidth\/Height() in Android SDK?<\/a><\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u539f\u6587\u51fa\u5904\uff1ahttp:\/\/threezj.com\/2015\/12\/17\/Android%20View%E8%AF%A6%E8%A7%A3\/<\/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\uff1a\u7ed8\u5236\u4ece\u6839\u5411\u4e0b\u8d70 Measure \u2192 Layout \u2192 Draw\uff1b\u81ea\u5b9a\u4e49\u6539 onMeasure\/onLayout\/onDraw\uff0c\u5bbd\u9ad8 API \u8981\u5206\u6e05\u6d4b\u91cf\u503c\u548c\u5e03\u5c40\u503c\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Android \u63a7\u4ef6\u5927\u81f4\u5206\u4e24\u7c7b\uff1aViewGroup \u5f53\u5bb9\u5668\u7ba1 View\uff0cView \u81ea\u5df1\u8d1f\u8d23\u7ed8\u5236\u3002\u6574\u68f5\u754c\u9762\u50cf D [&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\/1209"}],"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=1209"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1209\/revisions"}],"predecessor-version":[{"id":3555,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1209\/revisions\/3555"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}