{"id":255453,"date":"2016-03-27T20:40:33","date_gmt":"2016-03-27T12:40:33","guid":{"rendered":"http:\/\/blog.zhenglei.net\/?p=255453"},"modified":"2016-03-27T20:40:33","modified_gmt":"2016-03-27T12:40:33","slug":"%e8%bd%ac%ef%bc%9aandroid%e5%b8%83%e5%b1%80%e4%bc%98%e5%8c%96%e5%88%a9%e5%99%a8include%e5%92%8cviewstub","status":"publish","type":"post","link":"https:\/\/blog.zhenglei.net\/?p=255453","title":{"rendered":"\u8f6c\uff1aAndroid\u5e03\u5c40\u4f18\u5316\u5229\u5668include\u548cViewStub"},"content":{"rendered":"<p><a href=\"http:\/\/www.codeceo.com\/article\/android-include-viewstub.html\">http:\/\/www.codeceo.com\/article\/android-include-viewstub.html<\/a><\/p>\n<p>\u5f53\u521b\u5efa\u590d\u6742\u7684\u5e03\u5c40\u7684\u65f6\u5019\uff0c\u6709\u65f6\u5019\u4f1a\u53d1\u73b0\u6dfb\u52a0\u4e86\u5f88\u591a\u7684ViewGroup\u548cView\u3002\u968f\u4e4b\u800c\u6765\u7684\u95ee\u9898\u662fView\u6811\u7684\u5c42\u6b21\u8d8a\u6765\u8d8a\u6df1\uff0c\u5e94\u7528\u4e5f\u53d8\u7684\u8d8a\u6765\u8d8a\u6162\uff0c\u56e0\u4e3aUI\u6e32\u67d3\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u5c31\u5e94\u8be5\u8fdb\u884c\u5e03\u5c40\u4f18\u5316\u4e86\u3002\u8fd9\u91cc\u4ecb\u7ecd\u4e24\u79cd\u65b9\u5f0f\uff0c\u5206\u522b\u4e3a&lt;include&gt;\u6807\u7b7e\u548cViewStub\u7c7b\u3002<\/p>\n<h2>&lt;include\/&gt;<\/h2>\n<p>\u4f7f\u7528&lt;include\/&gt;\u662f\u4e3a\u4e86\u907f\u514d\u4ee3\u7801\u7684\u91cd\u590d\u3002\u8bbe\u60f3\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u9700\u8981\u4e3aapp\u4e2d\u7684\u6bcf\u4e2a\u89c6\u56fe\u90fd\u6dfb\u52a0\u4e00\u4e2afooter\uff0c\u8fd9\u4e2a footer\u662f\u4e00\u4e2a\u663e\u793aapp\u540d\u5b57\u7684TextView\u3002\u901a\u5e38\u591a\u4e2aActivity\u5bf9\u5e94\u591a\u4e2aXML\u5e03\u5c40\u6587\u4ef6\uff0c\u96be\u9053\u8981\u628a\u8fd9\u4e2aTextView\u590d\u5236\u5230\u6bcf\u4e2aXML \u4e2d\u5417\uff1f\u5982\u679cTextView\u9700\u8981\u505a\u4fee\u6539\uff0c\u90a3\u4e48\u6bcf\u4e2aXML\u5e03\u5c40\u6587\u4ef6\u90fd\u8981\u8fdb\u884c\u4fee\u6539\uff0c\u90a3\u7b80\u76f4\u662f\u5669\u68a6\u3002<\/p>\n<p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u5176\u4e2d\u4e00\u4e2a\u601d\u60f3\u5c31\u662f\u4ee3\u7801\u7684\u590d\u7528\uff0c\u90a3\u4e48\u600e\u4e48\u8fdb\u884c\u5e03\u5c40\u7684\u590d\u7528\u5462\uff1f\u8fd9\u65f6\uff0c&lt;include\/&gt;\u5c31\u8d77\u4f5c\u7528\u4e86\u3002<\/p>\n<p>\u5982\u679c\u5b66\u8fc7C\u8bed\u8a00\uff0c\u90a3\u4e48\u5bf9#include\u5e94\u8be5\u4e0d\u964c\u751f\uff0c\u5b83\u662f\u4e00\u4e2a\u9884\u7f16\u8bd1\u6307\u4ee4\uff0c\u5728\u7a0b\u5e8f\u7f16\u8bd1\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e4b\u524d\uff0c\u4f1a\u628a#include\u7684\u5185\u5bb9\u62f7\u8d1d\u5230#include\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>Android\u4e2d\u7684&lt;include\/&gt;\u4e5f\u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3\uff0c\u5c31\u662f\u628a\u67d0\u4e9b\u901a\u7528\u7684xml\u4ee3\u7801\u62f7\u8d1d\u5230&lt;include\/&gt;\u6240\u5728\u7684\u5730\u65b9\u3002\u4ee5\u4e00\u4e2aActivity\u4e3a\u4f8b\u3002<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\" &gt;\r\n    &lt;TextView\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerInParent=\"true\"\r\n        android:gravity=\"center_horizontal\"\r\n        android:text=\"@string\/hello\" \/&gt;\r\n\r\n    &lt;include layout=\"@layout\/footer_with_layout_properties\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p>footer_with_layout_properties.xml\u4e2d\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684TextView\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;TextView xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_alignParentBottom=\"true\"\r\n    android:layout_marginBottom=\"30dp\"\r\n    android:gravity=\"center_horizontal\"\r\n    android:text=\"@string\/footer_text\" \/&gt;<\/pre>\n<p>\u4e0a\u8ff0\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86&lt;include\/&gt;\u6807\u7b7e\uff0c\u8fbe\u5230\u4e86\u4ee3\u7801\u590d\u7528\u7684\u76ee\u7684\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u4ecd\u7136\u5b58\u5728\u4e00\u4e9b\u7591\u60d1\u3002<\/p>\n<p>footer_with_layout_properties.xml\u4e2d\u4f7f\u7528\u4e86android:layout_alignParentBottom\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u4e4b\u6240\u4ee5\u53ef\u884c\uff0c\u662f\u56e0\u4e3a\u5916\u5c42\u5e03\u5c40\u662fRelativeLayout\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5982\u679c\u5916\u5c42\u5e03\u5c40\u6362\u505aLinearLayout\u53c8\u4f1a\u600e\u6837\u5462\uff1f\u7b54\u6848\u663e\u800c\u6613\u89c1\uff0c\u8fd9\u80af\u5b9a\u662f\u884c\u4e0d\u901a\u7684\u3002\u90a3\u4e48\u600e\u4e48\u529e\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u628a\u5177\u4f53\u7684\u5c5e\u6027\u5199\u5728&lt;include\/&gt;\u6807\u7b7e\u91cc\u9762\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\"&gt;\r\n    &lt;TextView\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerInParent=\"true\"\r\n        android:gravity=\"center_horizontal\"\r\n        android:text=\"@string\/hello\"\/&gt;\r\n    &lt;include\r\n        layout=\"@layout\/footer\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentBottom=\"true\"\r\n        android:layout_marginBottom=\"30dp\"\/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p>\u6211\u4eec\u76f4\u63a5\u5728&lt;include\/&gt;\u6807\u7b7e\u91cc\u9762\u4f7f\u7528\u4e86android:layout_*\u5c5e\u6027\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u60f3\u8981\u5728&lt;include\/&gt;\u6807\u7b7e\u4e2d\u8986\u76d6\u88ab\u5305\u542b\u5e03\u5c40\u6240\u6307\u5b9a\u7684\u4efb\u4f55android:layout_*\u5c5e\u6027\uff0c\u5fc5\u987b \u5728&lt;include\/&gt;\u6807\u7b7e\u4e2d\u540c\u65f6\u6307\u5b9alayout_width\u548clayout_height\u5c5e\u6027\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2aAndroid\u7cfb\u7edf\u7684\u4e00\u4e2a bug\u5427\u3002<\/p>\n<h2>ViewStub<\/h2>\n<p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u5404\u79cd\u4ea4\u4e92\u95ee\u9898\uff0c\u4f8b\u5982\u663e\u793a\u6216\u9690\u85cf\u67d0\u4e2a\u89c6\u56fe\u3002\u5982\u679c\u60f3\u8981\u4e00\u4e2a\u89c6\u56fe\u53ea\u5728\u9700\u8981\u7684\u65f6\u5019\u663e\u793a\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528ViewStub\u8fd9\u4e2a\u7c7b\u3002<\/p>\n<p>\u5148\u770b\u4e00\u4e0bViewStub\u7684\u5b98\u65b9\u4ecb\u7ecd\uff1a<\/p>\n<p>\u201cViewStub\u662f\u4e00\u4e2a\u4e0d\u53ef\u89c6\u5e76\u4e14\u5927\u5c0f\u4e3a0\u7684\u89c6\u56fe\uff0c\u53ef\u4ee5\u5ef6\u8fdf\u5230\u8fd0\u884c\u65f6\u586b\u5145\u5e03\u5c40\u8d44\u6e90\u3002\u5f53ViewStub\u8bbe\u7f6e\u4e3aVisible\u6216\u8c03\u7528inflate()\u4e4b\u540e\uff0c\u5c31\u4f1a\u586b\u5145\u5e03\u5c40\u8d44\u6e90\uff0cViewStub\u4fbf\u4f1a\u88ab\u586b\u5145\u7684\u89c6\u56fe\u66ff\u4ee3\u201d\u3002<\/p>\n<p>\u73b0\u5728\u5df2\u7ecf\u6e05\u695aViewStub\u80fd\u5e72\u4ec0\u4e48\u4e86\uff0c\u90a3\u4e48\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\u4e00\u4e2a\u5e03\u5c40\u4e2d\uff0c\u5b58\u5728\u4e00\u4e2aMapView\uff0c\u53ea\u6709\u9700\u8981\u5b83\u7684\u65f6\u5019\uff0c\u624d\u8ba9\u5b83\u663e\u793a\u51fa\u6765\u3002<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\" &gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center_vertical\"\r\n        android:onClick=\"onShowMap\"\r\n        android:text=\"@string\/show_map\" \/&gt;\r\n\r\n    &lt;ViewStub\r\n        android:id=\"@+id\/map_stub\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"fill_parent\"\r\n        android:inflatedId=\"@+id\/map_view\"\r\n        android:layout=\"@layout\/map\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p>map.xml\u6587\u4ef6\u4e2d\u5305\u542b\u4e00\u4e2aMapView\uff0c\u53ea\u6709\u5728\u5fc5\u8981\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u8ba9\u5b83\u663e\u793a\u51fa\u6765\u3002<\/p>\n<pre>&lt;com.google.android.maps.MapView xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:id=\"@+id\/map_view\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\"\r\n    android:apiKey=\"my_api_key\"\r\n    android:clickable=\"true\" \/&gt;<\/pre>\n<p>\u53e6\u5916\uff0cinflatedId\u662fViewStub\u88ab\u8bbe\u7f6e\u6210Visible\u6216\u8c03\u7528inflate()\u65b9\u6cd5\u540e\u8fd4\u56de\u7684id\uff0c\u8fd9\u4e2aid\u5c31\u662f\u88ab\u586b\u5145\u7684View\u7684id\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c31\u662fMapView\u7684id\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u770b\u770bViewStub\u662f\u600e\u4e48\u4f7f\u7528\u7684\u3002<\/p>\n<pre>public class MainActivity extends MapActivity {\r\n\r\n  private View mViewStub;\r\n\r\n  @Override\r\n  public void onCreate(Bundle savedInstanceState) {\r\n    super.onCreate(savedInstanceState);\r\n    setContentView(R.layout.main);\r\n    mViewStub = findViewById(R.id.map_stub);\r\n  }\r\n\r\n  public void onShowMap(View v) {\r\n    mViewStub.setVisibility(View.VISIBLE);\r\n  }\r\n\r\n  @Override\r\n  protected boolean isRouteDisplayed() {\r\n    return false;\r\n  }\r\n}<\/pre>\n<h2>\u9898\u5916\u8bdd<\/h2>\n<p>\u6709\u7684\u540c\u5b66\u80af\u5b9a\u4f1a\u95ee\uff0c\u4f7f\u7528ViewStub\u548c\u5355\u7eaf\u5730\u628aView\u8bbe\u7f6e\u4e3aView.GONE\u6216View.VISIBLE\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f\u4e0d\u90fd\u662f\u663e\u793a\u548c\u9690\u85cf\u5417\uff0c\u4f7f\u7528ViewStub\u53cd\u800c\u66f4\u9ebb\u70e6\u4e86\u3002<\/p>\n<p>\u786e\u5b9e\u662f\u6709\u533a\u522b\u7684\uff0c\u4f1a\u6d89\u53ca\u5230View\u6811\u7684\u6e32\u67d3\uff0c\u5185\u5b58\u6d88\u8017\u7b49\u3002<\/p>\n<p>\u81f3\u4e8e\u6709\u4ec0\u4e48\u5177\u4f53\u7684\u5dee\u522b\uff0c\u5c31\u8bf7\u5927\u5bb6\u81ea\u5df1\u53bbGoogle\u5427\u3002\u4fd7\u8bdd\u8bf4\uff0c\u81ea\u5df1\u52a8\u624b\uff0c\u4e30\u8863\u8db3\u98df\u561b\uff01<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p>http:\/\/code.google.com\/p\/android\/issues\/detail?id=2863<\/p>\n<p>http:\/\/android-developers.blogspot.com.ar\/2009\/03\/android-layout-tricks-3-optimize-with.html<\/p>\n<p>http:\/\/developer.android.com\/reference\/android\/view\/ViewStub.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.codeceo.com\/article\/android-i &hellip; <a href=\"https:\/\/blog.zhenglei.net\/?p=255453\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[58],"class_list":["post-255453","post","type-post","status-publish","format-standard","hentry","category-software-download","tag-android-2"],"_links":{"self":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts\/255453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=255453"}],"version-history":[{"count":2,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts\/255453\/revisions"}],"predecessor-version":[{"id":255455,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts\/255453\/revisions\/255455"}],"wp:attachment":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}