{"id":456,"date":"2025-03-14T17:11:04","date_gmt":"2025-03-14T09:11:04","guid":{"rendered":"https:\/\/trantor.ink\/?p=456"},"modified":"2025-03-14T17:11:04","modified_gmt":"2025-03-14T09:11:04","slug":"android-viewstub-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=456","title":{"rendered":"Android ViewStub \u4f7f\u7528"},"content":{"rendered":"\n<p>ViewStub \u662f Android \u63d0\u4f9b\u7684\u4e00\u4e2a\u8f7b\u91cf\u7ea7 View\uff0c\u7528\u4e8e\u5ef6\u8fdf\u52a0\u8f7d\u5e03\u5c40\uff0c\u4f18\u5316\u6027\u80fd\u3002\u5b83\u5728 XML \u4e2d\u5b9a\u4e49\u65f6\u4e0d\u4f1a\u7acb\u5373\u5360\u7528\u8d44\u6e90\uff0c\u53ea\u6709\u5728 inflate() \u4e4b\u540e\u624d\u4f1a\u771f\u6b63\u521b\u5efa\u548c\u6dfb\u52a0\u89c6\u56fe\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1. ViewStub \u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<p>\u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>\u2022 <strong>\u51cf\u5c11\u521d\u59cb\u5316\u5f00\u9500<\/strong>\uff1a\u4ec5\u5728\u9700\u8981\u65f6\u52a0\u8f7d UI \u7ec4\u4ef6\uff0c\u907f\u514d\u521d\u59cb\u5316\u5927\u91cf\u89c6\u56fe\u3002<\/p>\n\n\n\n<p>\u2022 <strong>\u4f18\u5316\u6027\u80fd<\/strong>\uff1a\u51cf\u5c11 View \u5c42\u7ea7\uff0c\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u7684 RecyclerView \u9879\u5e03\u5c40\u3002<\/p>\n\n\n\n<p>\u2022 <strong>\u52a8\u6001\u5e03\u5c40<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u72b6\u6001\u52a8\u6001\u52a0\u8f7d\u5e03\u5c40\uff0c\u5982\u9519\u8bef\u63d0\u793a\u9875\u3001\u52a0\u8f7d\u72b6\u6001\u9875\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2. ViewStub \u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n\n\n\n<p><strong>\uff081\uff09\u5728 XML \u4e2d\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;ViewStub\n    android:id=\"@+id\/viewStub\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"wrap_content\"\n    android:layout=\"@layout\/layout_stub_content\"\/><\/pre>\n\n\n\n<p>\u2022 android:id\uff1a\u552f\u4e00 ID\uff0c\u7528\u4e8e\u5728\u4ee3\u7801\u4e2d\u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u2022 android:layout\uff1a\u5ef6\u8fdf\u52a0\u8f7d\u7684\u5e03\u5c40\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n\n\n\n<p>\u2022 layout_width \/ layout_height\uff1a\u53ef\u4ee5\u8bbe\u7f6e\u5177\u4f53\u5c3a\u5bf8\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 wrap_content\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\uff082\uff09\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u52a0\u8f7d<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val viewStub = findViewById&lt;ViewStub>(R.id.viewStub)\nval inflatedView = viewStub.inflate() \/\/ \u52a0\u8f7d\u5e03\u5c40\uff0c\u5e76\u8fd4\u56de\u6839 View<\/pre>\n\n\n\n<p>\u2022 inflate() \u65b9\u6cd5\u4f1a\u5c06 ViewStub \u66ff\u6362\u6210\u5b9e\u9645\u7684\u5e03\u5c40\uff0c\u5e76\u8fd4\u56de\u6839 View\u3002<\/p>\n\n\n\n<p>\u2022 \u8c03\u7528 inflate() \u540e\uff0cViewStub \u672c\u8eab\u4f1a\u88ab\u79fb\u9664\uff0c\u4e0d\u518d\u5360\u7528\u5185\u5b58\u3002<\/p>\n\n\n\n<p><strong>\u83b7\u53d6\u5b50\u63a7\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val textView = inflatedView.findViewById&lt;TextView>(R.id.textView)\ntextView.text = \"ViewStub \u52a0\u8f7d\u6210\u529f\uff01\"<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>3. \u4f7f\u7528 setVisibility() \u663e\u793a ViewStub<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 setVisibility(View.VISIBLE) \u6765\u89e6\u53d1 inflate()\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">viewStub.visibility = View.VISIBLE  \/\/ \u5185\u90e8\u4f1a\u81ea\u52a8\u8c03\u7528 inflate()<\/pre>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<\/p>\n\n\n\n<p>\u2022 ViewStub \u53ea\u80fd inflate() <strong>\u4e00\u6b21<\/strong>\uff0c\u4e4b\u540e\u4f1a\u88ab\u79fb\u9664\uff0c\u4e0d\u80fd\u518d setVisibility(View.GONE) \u518d\u6b21\u663e\u793a\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u9700\u8981 <strong>\u591a\u6b21\u5207\u6362<\/strong> \u89c6\u56fe\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (viewStub.parent != null) {\n    viewStub.inflate()  \/\/ \u53ea\u5728\u7b2c\u4e00\u6b21\u52a0\u8f7d\n} else {\n    inflatedView.visibility = View.VISIBLE  \/\/ \u4e4b\u540e\u76f4\u63a5\u663e\u793a\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>4. \u793a\u4f8b\uff1a\u70b9\u51fb\u6309\u94ae\u663e\u793a ViewStub<\/strong><\/p>\n\n\n\n<p><strong>XML \u5e03\u5c40<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;Button\n    android:id=\"@+id\/btnShow\"\n    android:layout_width=\"wrap_content\"\n    android:layout_height=\"wrap_content\"\n    android:text=\"\u663e\u793a ViewStub\" \/>\n\n&lt;ViewStub\n    android:id=\"@+id\/viewStub\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"wrap_content\"\n    android:layout=\"@layout\/layout_stub_content\"\/><\/pre>\n\n\n\n<p><strong>Kotlin \u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val btnShow = findViewById&lt;Button>(R.id.btnShow)\nval viewStub = findViewById&lt;ViewStub>(R.id.viewStub)\n\nbtnShow.setOnClickListener {\n    if (viewStub.parent != null) {\n        val inflatedView = viewStub.inflate() \/\/ \u52a0\u8f7d\u5e03\u5c40\n        val textView = inflatedView.findViewById&lt;TextView>(R.id.textView)\n        textView.text = \"ViewStub \u5df2\u52a0\u8f7d\"\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>5. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<p>\u2022 ViewStub <strong>\u521d\u59cb\u4e0d\u4f1a\u5360\u7528\u8d44\u6e90<\/strong>\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u2022 inflate() <strong>\u4f1a\u79fb\u9664 ViewStub \u672c\u8eab<\/strong>\uff0c\u4e0d\u80fd\u518d\u6b21 setVisibility(View.GONE) \u518d\u6b21\u663e\u793a\u3002<\/p>\n\n\n\n<p>\u2022 <strong>\u9002\u7528\u4e8e<\/strong> \u52a8\u6001\u5e03\u5c40\u3001\u72b6\u6001\u5207\u6362\uff08\u5982\u7a7a\u9875\u9762\u3001\u9519\u8bef\u9875\u9762\uff09\u3002<\/p>\n\n\n\n<p>\u2022 <strong>\u4e0d\u9002\u7528\u4e8e<\/strong> \u9700\u8981\u9891\u7e41\u5207\u6362\u7684 UI\uff0c\u53ef\u6539\u7528 ViewSwitcher \u6216 View.setVisibility()\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a0\u8f7d UI\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd \ud83d\ude80\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ViewStub \u662f Android \u63d0\u4f9b\u7684\u4e00\u4e2a\u8f7b\u91cf\u7ea7 View\uff0c\u7528\u4e8e\u5ef6\u8fdf\u52a0\u8f7d\u5e03\u5c40\uff0c\u4f18\u5316\u6027\u80fd\u3002\u5b83\u5728 XML \u4e2d\u5b9a\u4e49\u65f6\u4e0d\u4f1a\u7acb\u5373\u5360\u7528\u8d44\u6e90\uff0c\u53ea\u6709\u5728 inflate() \u4e4b\u540e\u624d\u4f1a\u771f\u6b63\u521b\u5efa\u548c\u6dfb\u52a0\u89c6\u56fe\u3002 1. ViewStub \u7684\u4f7f\u7528\u573a\u666f \u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a \u2022 \u51cf\u5c11\u521d\u59cb\u5316\u5f00\u9500\uff1a\u4ec5\u5728\u9700\u8981\u65f6\u52a0\u8f7d UI \u7ec4\u4ef6\uff0c\u907f\u514d\u521d\u59cb\u5316\u5927\u91cf\u89c6\u56fe\u3002 \u2022 \u4f18\u5316\u6027\u80fd\uff1a\u51cf\u5c11 View \u5c42\u7ea7\uff0c\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u7684 RecyclerView \u9879\u5e03\u5c40\u3002 \u2022 \u52a8\u6001\u5e03\u5c40\uff1a\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u72b6\u6001\u52a8\u6001\u52a0\u8f7d\u5e03\u5c40\uff0c\u5982\u9519\u8bef\u63d0\u793a\u9875\u3001\u52a0\u8f7d\u72b6\u6001\u9875\u7b49\u3002 2. ViewStub \u7684\u57fa\u672c\u7528\u6cd5 \uff081\uff09\u5728 XML \u4e2d\u5b9a\u4e49 \u2022 android:id\uff1a\u552f\u4e00 ID\uff0c\u7528\u4e8e\u5728\u4ee3\u7801\u4e2d\u5f15\u7528\u3002 \u2022 android:layout\uff1a\u5ef6\u8fdf\u52a0\u8f7d\u7684\u5e03\u5c40\u6587\u4ef6\u8def\u5f84\u3002 \u2022 layout_width \/ layout_height\uff1a\u53ef\u4ee5\u8bbe\u7f6e\u5177\u4f53\u5c3a\u5bf8\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 wrap_content\u3002 \uff082\uff09\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u52a0\u8f7d \u2022 inflate() \u65b9\u6cd5\u4f1a\u5c06 ViewStub \u66ff\u6362\u6210\u5b9e\u9645\u7684\u5e03\u5c40\uff0c\u5e76\u8fd4\u56de\u6839 View\u3002 \u2022 \u8c03\u7528 inflate() \u540e\uff0cViewStub \u672c\u8eab\u4f1a\u88ab\u79fb\u9664\uff0c\u4e0d\u518d\u5360\u7528\u5185\u5b58\u3002 \u83b7\u53d6\u5b50\u63a7\u4ef6 3. \u4f7f\u7528 setVisibility() \u663e\u793a ViewStub \u53ef\u4ee5\u4f7f\u7528 setVisibility(View.VISIBLE) \u6765\u89e6\u53d1 inflate()\uff1a \u6ce8\u610f\uff1a \u2022 ViewStub \u53ea\u80fd inflate() \u4e00\u6b21\uff0c\u4e4b\u540e\u4f1a\u88ab\u79fb\u9664\uff0c\u4e0d\u80fd\u518d setVisibility(View.GONE) \u518d\u6b21\u663e\u793a\u3002 \u5982\u679c\u9700\u8981 \u591a\u6b21\u5207\u6362 \u89c6\u56fe\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a 4. \u793a\u4f8b\uff1a\u70b9\u51fb\u6309\u94ae\u663e\u793a ViewStub XML \u5e03\u5c40 Kotlin \u4ee3\u7801 5. \u603b\u7ed3 \u2022 ViewStub \u521d\u59cb\u4e0d\u4f1a\u5360\u7528\u8d44\u6e90\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002 \u2022 inflate() \u4f1a\u79fb\u9664 ViewStub \u672c\u8eab\uff0c\u4e0d\u80fd\u518d\u6b21 setVisibility(View.GONE) \u518d\u6b21\u663e\u793a\u3002 \u2022 \u9002\u7528\u4e8e \u52a8\u6001\u5e03\u5c40\u3001\u72b6\u6001\u5207\u6362\uff08\u5982\u7a7a\u9875\u9762\u3001\u9519\u8bef\u9875\u9762\uff09\u3002 \u2022 \u4e0d\u9002\u7528\u4e8e \u9700\u8981\u9891\u7e41\u5207\u6362\u7684 UI\uff0c\u53ef\u6539\u7528 ViewSwitcher \u6216 View.setVisibility()\u3002 \u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a0\u8f7d UI\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd \ud83d\ude80\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[58],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-appdev","tag-android"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=456"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":457,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/456\/revisions\/457"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}