{"id":175026,"date":"2020-04-06T08:12:48","date_gmt":"2020-04-06T00:12:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=175026"},"modified":"2020-03-23T09:15:01","modified_gmt":"2020-03-23T01:15:01","slug":"android-access-explanation","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/android-access-explanation.html","title":{"rendered":"Android \u8d44\u6e90(Resources)\u8bbf\u95ee\u8bb2\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b89\u5353\uff08Android\uff09\u662f\u4e00\u79cd\u57fa\u4e8eLinux\u7684\u81ea\u7531\u53ca\u5f00\u653e\u6e90\u4ee3\u7801\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002\u4e3b\u8981\u4f7f\u7528\u4e8e\u79fb\u52a8\u8bbe\u5907\uff0c\u5982\u667a\u80fd\u624b\u673a\u548c\u5e73\u677f\u7535\u8111\uff0c\u7531Google\u516c\u53f8\u548c\u5f00\u653e\u624b\u673a\u8054\u76df\u9886\u5bfc\u53ca\u5f00\u53d1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u6709\u8bb8\u591a\u4e1c\u897f\u7528\u6765\u6784\u5efa\u4e00\u4e2a\u4f18\u79c0\u7684 Android \u5e94\u7528\u7a0b\u5e8f\u3002\u9664\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u7f16\u7801\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u5404\u79cd\u5404\u6837\u7684\u8d44\u6e90\uff0c\u8bf8\u5982\u4f60\u7528\u5230\u7684\u5404\u79cd\u9759\u6001\u5185\u5bb9\uff0c\u5982\u4f4d\u56fe\uff0c\u989c\u8272\uff0c\u5e03\u5c40\u5b9a\u4e49\uff0c\u7528\u6237\u754c\u9762\u5b57\u7b26\u4e32\uff0c\u52a8\u753b\u7b49\u7b49\u3002\u8fd9\u4e9b\u8d44\u6e90\u4e00\u822c\u653e\u7f6e\u5728\u9879\u76ee\u7684 res\/ \u4e0b\u72ec\u7acb\u5b50\u76ee\u5f55\u4e2d\u3002<\/p>\n

\u8fd9\u8282\u6559\u7a0b\u5c06\u5b66\u4e60\u5982\u4f55\u6765\u7ec4\u7ec7\u5e94\u7528\u7a0b\u5e8f\u8d44\u6e90\uff0c\u6307\u5b9a\u66ff\u4ee3\u8d44\u6e90\uff0c\u5e76\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n

\u5728eclipse\u4e2d\u7ec4\u7ec7\u8d44\u6e90<\/strong><\/div>\n

\u4f60\u9700\u8981\u5c06\u6bcf\u79cd\u8d44\u6e90\u653e\u7f6e\u5728\u9879\u76ee\u4e2d res\/ \u76ee\u5f55\u7684\u7279\u5b9a\u5b50\u76ee\u5f55\u4e0b\u3002\u4f8b\u5982\uff0c\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u9879\u76ee\u7684\u6587\u4ef6\u5c42\u7ea7\uff1a<\/p>\n

MyProject\/\r\n    src\/  \r\n        MyActivity.java  \r\n    res\/\r\n        drawable\/  \r\n            icon.png  \r\n        layout\/  \r\n            activity_main.xml\r\n            info.xml\r\n        values\/  \r\n            strings.xml <\/pre>\n

res\/ \u76ee\u5f55\u5728\u5404\u79cd\u5b50\u76ee\u5f55\u4e2d\u5305\u542b\u4e86\u6240\u6709\u7684\u8d44\u6e90\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u56fe\u7247\u8d44\u6e90\uff0c\u4e24\u4e2a\u5e03\u5c40\u8d44\u6e90\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d44\u6e90\u6587\u4ef6\u3002\u4e0b\u8868\u8be6\u7ec6\u7684\u7ed9\u51fa\u4e86\u5728\u9879\u76ee\u4e2d res\/ \u76ee\u5f55\u91cc\u9762\u652f\u6301\u7684\u8d44\u6e90\u3002
\n\"\"<\/p>\n

\u66ff\u4ee3\u8d44\u6e90<\/strong><\/div>\n

\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4e3a\u7279\u5b9a\u7684\u8bbe\u5907\u914d\u7f6e\u63d0\u4f9b\u66ff\u4ee3\u7684\u8d44\u6e90\u652f\u6301\u3002\u6bd4\u5982\u8bf4\uff0c\u4f60\u9700\u8981\u4e3a\u4e0d\u540c\u7684\u5c4f\u5e55\u5206\u8fa8\u7387\u63d0\u4f9b\u66ff\u4ee3\u7684\u56fe\u7247\u8d44\u6e90\uff0c\u4e3a\u4e0d\u540c\u7684\u8bed\u8a00\u63d0\u4f9b\u66ff\u4ee3\u7684\u5b57\u7b26\u4e32\u8d44\u6e90\u3002\u5728\u8fd0\u884c\u65f6\uff0cAndroid \u68c0\u6d4b\u5f53\u524d\u8bbe\u5907\u914d\u7f6e\uff0c\u5e76\u4e3a\u5e94\u7528\u7a0b\u5e8f\u52a0\u8f7d\u5408\u9002\u7684\u8d44\u6e90\u3002<\/p>\n

\u8981\u4e3a\u7279\u5b9a\u7684\u914d\u7f6e\u7684\u786e\u5b9a\u4e00\u7cfb\u5217\u66ff\u4ee3\u8d44\u6e90\uff0c\u9075\u5faa\u5982\u4e0b\u7684\u6b65\u9aa4\uff1a<\/p>\n

  • \u5728res\/ \u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\uff0c\u4ee5 _ \u7684\u65b9\u5f0f\u547d\u540d\u3002\u8fd9\u91cc\u7684 resources_name \u662f\u4e0a\u8868\u4e2d\u63d0\u5230\u7684\u4efb\u610f\u8d44\u6e90\uff0c\u5982\u5e03\u5c40\u3001\u56fe\u7247\u7b49\u3002 qualifier \u5c06\u786e\u5b9a\u4e2a\u6027\u7684\u914d\u7f6e\u4f7f\u7528\u54ea\u4e9b\u8d44\u6e90\u3002\u4f60\u53ef\u4ee5\u67e5\u770b\u5b98\u65b9\u6587\u6863\u6765\u4e86\u89e3\u4e0d\u540c\u7c7b\u578b\u8d44\u6e90\u7684\u4e00\u4e2a\u5b8c\u6574 qualifier \u5217\u8868\u3002<\/config_qualifier><\/resource_name><\/li>\n
  • \u5728\u8fd9\u4e2a\u76ee\u5f55\u4e2d\u4fdd\u5b58\u54cd\u5e94\u7684\u66ff\u4ee3\u8d44\u6e90\u3002\u8fd9\u4e9b\u8d44\u6e90\u6587\u4ef6\u5fc5\u987b\u4e0e\u4e0b\u9762\u4f8b\u5b50\u4e2d\u5c55\u793a\u7684\u9ed8\u8ba4\u8d44\u6e90\u6587\u4ef6\u540d\u4e00\u81f4\uff0c\u7136\u800c\u8fd9\u4e9b\u6587\u4ef6\u5c06\u786e\u5b9a\u7684\u5185\u5bb9\u8fdb\u884c\u66ff\u4ee3\u3002\u4f8b\u5982\uff1a\u867d\u7136\u56fe\u7247\u7684\u6587\u4ef6\u540d\u4e00\u6837\uff0c\u4f46\u662f\u9ad8\u5206\u8fa8\u7387\u7684\u5c4f\u5e55\uff0c\u56fe\u7247\u7684\u5206\u8fa8\u7387\u4e5f\u4f1a\u9ad8\u3002<\/li>\n

    \u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6307\u5b9a\u9ed8\u8ba4\u5c4f\u5e55\u7684\u56fe\u7247\u548c\u9ad8\u5206\u8fa8\u7387\u7684\u66ff\u4ee3\u56fe\u7247\u3002<\/p>\n

    \r\nMyProject\/\r\n   src\/\r\n    main\/\r\n    java\/\r\n       MyActivity.java  \r\n       res\/\r\n          drawable\/  \r\n            icon.png\r\n            background.png\r\n        drawable-hdpi\/  \r\n            icon.png\r\n            background.png  \r\n        layout\/  \r\n            activity_main.xml\r\n            info.xml\r\n        values\/  \r\n            strings.xml\r\n<\/pre>\n

    \u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6307\u5b9a\u9ed8\u8ba4\u8bed\u8a00\u7684\u5e03\u5c40\u548c\u963f\u62c9\u4f2f\u8bed\u8a00\u7684\u66ff\u4ee3\u5e03\u5c40\u3002<\/p>\n

    MyProject\/\r\n   src\/\r\n    main\/\r\n    java\/\r\n       MyActivity.java  \r\n      res\/\r\n         drawable\/  \r\n            icon.png\r\n            background.png\r\n        drawable-hdpi\/  \r\n            icon.png\r\n            background.png  \r\n        layout\/  \r\n            activity_main.xml\r\n            info.xml\r\n        layout-ar\/\r\n            main.xml\r\n        values\/  \r\n            strings.xml\r\n\r\n<\/pre>\n
    \u8bbf\u95ee\u8d44\u6e90<\/strong><\/div>\n

    \u5728\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u9700\u8981\u8bbf\u95ee\u5b9a\u4e49\u597d\u7684\u8d44\u6e90\uff0c\u4e0d\u8bba\u662f\u901a\u8fc7\u4ee3\u7801\u8fd8\u662f\u901a\u8fc7 XML \u6587\u4ef6\u3002\u4e0b\u9762\u7684\u7ae0\u8282\u4ecb\u7ecd\u5982\u4f55\u5206\u522b\u5728\u8fd9\u4e24\u79cd\u573a\u666f\u4e2d\u8bbf\u95ee\u8d44\u6e90\u3002<\/p>\n

    \u5728\u4ee3\u7801\u8bbf\u95ee\u8d44\u6e90<\/strong><\/div>\n

    \u5f53 Android \u5e94\u7528\u7a0b\u5e8f\u88ab\u7f16\u8bd1\uff0c\u751f\u6210\u4e00\u4e2a R \u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709 res\/ \u76ee\u5f55\u4e0b\u8d44\u6e90\u7684 ID\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 R \u7c7b\uff0c\u901a\u8fc7\u5b50\u7c7b+\u8d44\u6e90\u540d\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u8d44\u6e90 ID \u6765\u8bbf\u95ee\u8d44\u6e90\u3002<\/p>\n

    \u5b9e\u4f8b<\/strong><\/div>\n

    \u8bbf\u95ee res\/drawable\/myimage.png\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u5230 ImageView \u4e0a\uff0c\u4f60\u5c06\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

    ImageView imageView = (ImageView) findViewById(R.id.myimageview);\r\nimageView.setImageResource(R.drawable.myimage);<\/pre>\n

    \u8fd9\u91cc\u7b2c\u4e00\u884c\u4ee3\u7801\u7528 R.id.myimageview \u6765\u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\u83b7\u53d6\u5b9a\u4e49\u4e3a myimageview \u7684 ImageView\u3002\u7b2c\u4e8c\u884c\u7528 R.drawable.myimage \u6765\u83b7\u53d6\u5728 res\/ \u7684 drawable \u5b50\u76ee\u5f55\u4e0b\u540d\u4e3a myimage \u7684\u56fe\u7247\u3002<\/p>\n

    \u5b9e\u4f8b<\/strong><\/div>\n

    \u8003\u8651\u4e0b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5176\u4e2d res\/values\/strings.xml \u6709\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n

    \u73b0\u5728\u4f60\u53ef\u4ee5\u5728 ID \u4e3a msg \u7684 TextView \u5bf9\u8c61\u4e0a\u4f7f\u7528\u8d44\u6e90 ID \u6765\u8bbe\u7f6e\u6587\u672c\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n

    TextView msgTextView = (TextView) findViewById(R.id.msg);\r\nmsgTextView.setText(R.string.hello);<\/pre>\n
    \u5b9e\u4f8b<\/strong><\/div>\n

    \u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u5c06\u4e3a\u6d3b\u52a8\u52a0\u8f7d\u8fd9\u4e2a\u5e03\u5c40\uff0conCreate() \u65b9\u6cd5\u4e2d\u5982\u4e0b\uff1a<\/p>\n

    public void onCreate(Bundle savedInstanceState) {\r\n   super.onCreate(savedInstanceState);\r\n   setContentView(R.layout.main_activity);\r\n}<\/pre>\n
    \u5728XML\u4e2d\u8bbf\u95ee<\/strong><\/div>\n

    \u8003\u8651\u4e0b\u9762\u7684 XML \u8d44\u6e90\u6587\u4ef6 res\/values\/strings.xml\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u989c\u8272\u8d44\u6e90\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d44\u6e90 -<\/p>\n

    <?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<resources>\r\n   <color name=\"opaque_red\">#f00<\/color>\r\n   <string name=\"hello\">Hello!<\/string>\r\n<\/resources>\r\n<\/pre>\n

    \u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u8d44\u6e90\u6765\u8bbe\u7f6e\u6587\u672c\u989c\u8272\u548c\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n

    <?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<EditText xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\"\r\n    android:textColor=\"@color\/opaque_red\"\r\n    android:text=\"@string\/hello\" \/>\r\n\r\n<\/pre>\n

    \u73b0\u5728\uff0c\u4f60\u5982\u679c\u518d\u6b21\u56de\u5230\u4e0a\u4e00\u7ae0\u8282\u8bb2\u89e3\u7684\" Hello World! \"\u5b9e\u4f8b\uff0c\u6211\u53ef\u4ee5\u786e\u5b9a\uff0c\u4f60\u5bf9\u8fd9\u8282\u4e2d\u6240\u6709\u7684\u6982\u5ff5\u6709\u4e86\u66f4\u597d\u7684\u7406\u89e3\u3002\u6240\u4ee5\uff0c\u6211\u5f3a\u70c8\u5efa\u8bae\u56de\u53bb\u770b\u770b\u4e4b\u524d\u7684\u5b9e\u4f8b\uff0c\u5e76\u67e5\u770b\u6211\u4f7f\u7528\u4e0d\u540c\u8d44\u6e90\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6709\u8bb8\u591a\u4e1c\u897f\u7528\u6765\u6784\u5efa\u4e00\u4e2a\u4f18\u79c0\u7684 Android \u5e94\u7528\u7a0b\u5e8f\u3002\u9664\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u7f16\u7801\uff0c\u4f60\u9700\u8981\u5173\u6ce8\u5404\u79cd\u5404\u6837\u7684\u8d44\u6e90\uff0c\u8bf8\u5982\u4f60\u7528 […]<\/p>\n","protected":false},"author":1481,"featured_media":175009,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-175026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=175026"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175026\/revisions"}],"predecessor-version":[{"id":175028,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175026\/revisions\/175028"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/175009"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=175026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=175026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=175026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}