{"id":1120,"date":"2010-10-08T15:25:00","date_gmt":"2010-10-08T07:25:00","guid":{"rendered":"\/?p=1120"},"modified":"2010-10-08T15:25:00","modified_gmt":"2010-10-08T15:25:00","slug":"1120","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/other\/1120.html","title":{"rendered":"Android UI \u7528\u6237\u754c\u9762\u5f00\u53d1\u57fa\u672c\u6982\u5ff5\u6982\u89c8"},"content":{"rendered":"
\u672c\u6587\u4e3b\u8981\u5bf9Android UI \u7528\u6237\u754c\u9762\u5f00\u653e\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\uff0c\u5747\u4e0d\u505a\u6df1\u5165\u8bb2\u89e3\uff0c\u4f46\u53ef\u4ee5\u5feb\u901f\u6d4f\u89c8Android\u5f00\u653e\u7ecf\u5e38\u6d89\u53ca\u5230\u7684\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u8981\u7d20\u3002<\/p>\n
\u4e00\u3001\u754c\u9762\u5143\u7d20<\/strong><\/span><\/p>\n 1. \u7a97\u53e3\uff1aActivity<\/p>\n \u5e94\u7528\u7a0b\u5e8f\u4e2d\u6bcf\u4e2a\u5c4f\u5e55\u663e\u793a\u90fd\u901a\u8fc7\u7ee7\u627f\u548c\u6269\u5c55\u57fa\u7c7bActivity\u5b9e\u73b0<\/p>\n \u5206\u7c7b\uff1a\u57fa\u672cActivity\uff0c\u5e26\u5185\u5bb9\u7684Activity\uff08\u5982ListActivity\uff09<\/p>\n 2. \u5bf9\u8bdd\u6846\uff1aDialog<\/p>\n \u4e00\u79cd\u663e\u793a\u4e8eActivity\u4e4b\u4e0a\u7684\u754c\u9762\u5143\u7d20\uff0c\u7531Activity\u7ba1\u7406\uff0c\u7528\u4e8e\u663e\u793a\u4e00\u4e9b\u4e34\u65f6\u4fe1\u606f\u548c\u529f\u80fd\uff0c\u5b83\u6bd4Activity\u5f00\u9500\u5c0f<\/p>\n 1) \u5e38\u7528\u5bf9\u8bdd\u6846\uff1a<\/p>\n AlertDialog\uff1a\u4fe1\u606f\u5bf9\u8bdd\u6846<\/p>\n DatePickerDialog\uff0cTimePickerDialog\uff1a\u65f6\u95f4\u65e5\u671f\u9009\u62e9\u5bf9\u8bdd\u6846<\/p>\n ProgressDialog\uff1a\u8fdb\u5ea6\u5bf9\u8bdd\u6846<\/p>\n \u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u5bf9\u8bdd\u6846\u5185\u5bb9\u4e3a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684View<\/p>\n 2) \u76f8\u5173\u51fd\u6570\uff1a<\/p>\n onCreateDialog()\uff1a\u521b\u5efa\u5bf9\u8bdd\u6846\u7684\u5b9e\u73b0<\/p>\n showDialog()\uff1a\u9700\u8981\u663e\u793a\u7684\u65f6\u5019\u8c03\u7528<\/p>\n onPrepareDialog()\uff1a\u66f4\u6539\u5df2\u6709\u5bf9\u8bdd\u6846\u65f6\u8c03\u7528<\/p>\n 3. \u83dc\u5355\uff1aMenu<\/p>\n \u4e00\u79cd\u663e\u793a\u4e8eActivity\u4e4b\u4e0a\u7684\u754c\u9762\u5143\u7d20\uff0c\u7531Activity\u7ba1\u7406\uff0c\u7528\u4e8e\u63d0\u4f9b\u7528\u6237\u4e00\u4e9b\u989d\u5916\u7684\u9009\u9879\u548c\u8bbe\u7f6e<\/p>\n 1) \u5e38\u7528\u83dc\u5355\uff1a<\/p>\n Options Menu\uff1a\u6309Menu\u952e\u65f6\u4ece\u5c4f\u5e55\u5e95\u90e8\u6ed1\u51fa\u7684\u83dc\u5355<\/p>\n Context Menu\uff1a\u7528\u6237\u901a\u8fc7\u5728\u6309\u4ef6\u4e0a\u957f\u6309\u8c03\u51fa\u5b83<\/p>\n Submenu\uff1a\u7528\u4e8e\u7ed9\u83dc\u5355\u5206\u7c7b\uff0c\u53ef\u88ab\u6dfb\u52a0\u5230\u5176\u5b83\u83dc\u5355\u4e0a\uff0c\u4e0d\u80fd\u6dfb\u52a0\u5230\u5b50\u83dc\u5355\u4e0a<\/p>\n 2) \u76f8\u5173\u51fd\u6570\uff1a<\/p>\n onCreateOptionMenu()\uff1a\u521b\u5efa\u83dc\u5355 \uff08onCreateContextMenu\u2026\uff09<\/p>\n onOptionsItemSelected()\uff1a\u5904\u7406\u83dc\u5355\u9879\u7684\u5355\u51fb\u4e8b\u4ef6<\/p>\n onPrepareOptionsMenu()\uff1a\u52a8\u6001\u66f4\u65b0<\/p>\n 4. \u53ef\u89c6\u5316\u63a7\u4ef6\uff1aView<\/span><\/strong><\/p>\n View\u662f\u53ef\u89c6\u5316\u63a7\u4ef6\u7684\u57fa\u7c7b<\/span><\/strong><\/p>\n 1) \u5e38\u7528\u63a7\u4ef6\uff1a<\/p>\n TextView, ImageView<\/p>\n Button, CheckBox, RadioButton, ImageButton, ToggleButton<\/p>\n AnalogClock, DigitalClock<\/p>\n EditText, List\u2026<\/p>\n 2) \u6570\u636e\u7ed1\u5b9a\uff1aadapter<\/p>\n adapter\u5c06\u63a7\u4ef6\uff08\u5982List, GridView\uff09\u548c\u590d\u6742\u6570\u636e\uff08\u5982\u6570\u636e\uff0c\u952e\u8868\uff09\u7ed1\u5b9a\u8d77\u6765<\/p>\n 5. \u5176\u5b83\u754c\u9762\u5143\u7d20<\/p>\n \u6807\u9898\u680f<\/p>\n<\/p>\n \u4e8c\u3001layout\u5e03\u5c40\u7684\u5b9e\u73b0<\/strong><\/span><\/p>\n \u8bbe\u7f6e\u63a7\u4ef6\u5728\u5c4f\u5e55\u4e0a\u5982\u4f55\u6392\u5e03<\/p>\n 1. LinearLayout\uff1a\u7ebf\u6027\u7684\u5e03\u5c40\u65b9\u5f0f<\/p>\n \u6700\u5e38\u7528\uff0c\u4e0a\u4e0b\u6216\u5de6\u53f3\u7684\u6dfb\u52a0\u63a7\u4ef6<\/p>\n 2. GridView\uff1a\u7f51\u683c\u5e03\u5c40<\/p>\n \u63a7\u4ef6\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u586b\u5230\u6bcf\u4e2a\u683c\u5b50\u91cc\u5c31\u597d\u4e86\uff0c\u4f7f\u754c\u9762\u5f88\u6574\u9f50<\/p>\n 3. TableLayout\uff1a\u8868\u683c\u5e03\u5c40<\/p>\n \u4ee5\u884c\u5217\u5f62\u5f0f\u7ba1\u7406\u5b50\u63a7\u4ef6\uff0c\u6bcf\u884c\u4e3a\u4e00\u4e2aTableRow\uff0cTableRow\u53ef\u6dfb\u52a0\u5b50\u63a7\u4ef6<\/p>\n 4. AbsoluteLayout\uff1a\u7edd\u5bf9\u5750\u6807\u5e03\u5c40<\/p>\n \u53ef\u4ee5\u8ba9\u5b50\u5143\u7d20\u6307\u5b9a\u51c6\u786e\u7684x\/y\u5750\u6807\u503c\uff0c\u5e76\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002(0, 0)\u4e3a\u5de6\u4e0a\u89d2\u3002<\/p>\n AbsoluteLayout\u6ca1\u6709\u9875\u8fb9\u6846\uff0c\u5141\u8bb8\u5143\u7d20\u4e4b\u95f4\u4e92\u76f8\u91cd\u53e0\uff08\u5c3d\u7ba1\u4e0d\u63a8\u8350\uff09\u3002<\/p>\n \u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5176\u5728\u4e0d\u540c\u7684\u8bbe\u5907\u4e0a\u53ef\u80fd\u4e0d\u80fd\u5f88\u597d\u5730\u5de5\u4f5c\u3002<\/p>\n 5. RelativeLayout\uff1a\u76f8\u5bf9\u5750\u6807\u5e03\u5c40<\/p>\n \u63a7\u4ef6\u53ef\u6307\u5b9a\u4ed6\u4eec\u76f8\u5bf9\u4e8e\u5176\u5b83\u5143\u7d20\u6216\u7236\u5143\u7d20\u7684\u4f4d\u7f6e\uff08\u901a\u8fc7ID\u6307\u5b9a\uff09\u3002<\/p>\n \u53ef\u4ee5\u4ee5\u53f3\u5bf9\u9f50\uff0c\u6216\u4e0a\u4e0b\uff0c\u6216\u7f6e\u4e8e\u5c4f\u5e55\u4e2d\u592e\u7684\u5f62\u5f0f\u6765\u6392\u5217\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n 6. FrameLayout\uff1a\u5355\u5e27\u5e03\u5c40<\/p>\n \u6240\u6709\u7684\u5b50\u5143\u7d20\u5c06\u4f1a\u56fa\u5b9a\u5728\u5c4f\u5e55\u7684\u5de6\u4e0a\u89d2\uff0c\u540e\u4e00\u4e2a\u5b50\u5143\u7d20\u5c06\u4f1a\u76f4\u63a5\u5728\u524d\u4e00\u4e2a\u5b50\u5143\u7d20\u4e4b\u4e0a\u8fdb\u884c\u8986\u76d6\u586b\u5145<\/p>\n<\/p>\n \u4e09\u3001\u4e8b\u4ef6\u54cd\u5e94<\/strong><\/span><\/p>\n 1. \u7528\u6ce8\u518c\u56de\u8c03\u51fd\u6570\u7684\u65b9\u5f0f\u54cd\u5e94\u4e8b\u4ef6<\/p>\n 2. \u5982\u679c\u6ca1\u88ab\u4efb\u4f55\u4e00\u4e2aView\u5904\u7406\uff0c\u5219\u7531Activity\u5904\u7406<\/p>\n 3. \u5e38\u7528\u4e8b\u4ef6\u5904\u7406<\/p>\n 1) onKeyDown()<\/p>\n 2) onKeyUp()<\/p>\n 3) onTrackballEvent()<\/p>\n 4) onTouchEvent()<\/p>\n<\/p>\n \u56db\u3001\u5e94\u7528\u8f6f\u4ef6\u4ee3\u7801\u76ee\u5f55\u7ed3\u6784<\/span><\/strong><\/p>\n 1. res\uff1a\u8d44\u6e90<\/span><\/p>\n \u8d44\u6e90\u662f\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5230\u7684\u5e76\u4e14\u5728\u7f16\u8bd1\u65f6\u88ab\u6253\u5305\u8fdb\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u9644\u52a0\u6587\u4ef6<\/p>\n \u5728\u4ee3\u7801\u4e2d\u901a\u8fc7R\u7c7b\u8c03\u7528(R\u7c7b\u81ea\u52a8\u751f\u6210\uff0c\u5f62\u5982R.string.title)<\/p>\n 1) layout\uff1a\u5b58\u653e\u5e03\u5c40\u7528\u7684xml\u6587\u4ef6<\/p>\n a) \u5e03\u5c40\u5c31\u50cf\u5bb9\u5668\uff0c\u91cc\u9762\u53ef\u4ee5\u88c5\u4e0b\u5f88\u591a\u63a7\u4ef6\uff0c\u6bcf\u4e2a\u63a7\u4ef6\u53c8\u6709\u5e03\u5c40\uff0c\u5b57\u4f53\u8bbe\u5b9a\uff0c<\/p>\n \u5982\u5b57\u4f53\u5927\u5c0f\u5728layout\u7684xml\u91cc\u4f7f\u7528\uff1a<\/p>\n Android:textAppearance=\u201d?android:attr\/textAppearanceSmall\u201d<\/p>\n b) \u9ed8\u8ba4\u7684\u5e03\u5c40\u6587\u4ef6main.xml<\/p>\n 2) drawable\uff1a\u5b58\u653e\u56fe\u7247\u56fe\u6807<\/p>\n 3) values\uff1a\u5b58\u653e\u5e38\u91cf\u7684xml\u6587\u4ef6\uff08\u5982\u5b57\u4e32\uff09<\/p>\n 2. src\uff1a\u4ee3\u7801<\/span><\/p>\n 3. AndroidManifest.xml<\/span><\/span><\/p>\n \u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u6709\u54ea\u4e9b\u7ec4\u4ef6\uff0c\u54ea\u4e9b\u8d44\u6e90<\/span><\/p>\n 1) \u5e94\u7528\u7a0b\u5e8f\u7684java\u5305\u540d<\/p>\n 2) \u5e94\u7528\u7a0b\u5e8f\u4e2d\u6240\u542b\u7ec4\u4ef6\uff08Activity, Service, BroadcastReceiver, ContentProvider\uff09<\/p>\n 3) \u5e94\u7528\u7a0b\u5e8f\u8c03\u7528\u5176\u5b83\u7a0b\u5e8f\u65f6\u7684\u6743\u9650<\/p>\n 4) \u5e94\u7528\u7a0b\u5e8f\u88ab\u5176\u5b83\u7a0b\u5e8f\u8c03\u7528\u65f6\u5bf9\u5176\u5b83\u7a0b\u5e8f\u7684\u6743\u9650\u8981\u6c42<\/p>\n 5) \u4f7f\u7528Android API\u7684\u6700\u4f4e\u7248\u672c<\/p>\n 6) \u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u7684\u5e93<\/p>\n<\/p>\n \u4e94\u3001\u56fd\u9645\u5316\u7684\u652f\u6301\uff08\u591a\u8bed\u8a00\uff09<\/strong><\/span><\/p>\n 1. \u5728\u7cfb\u7edf\u4e2d\u5982\u4f55\u8bbe\u7f6e\u672c\u5730\u8bed\u8a00<\/p>\n \u684c\u9762->settings->Local & text->Select locale->Chinese(China)<\/p>\n 2. \u5728\u7a0b\u5e8f\u4e2d\u5982\u4f55\u52a0\u5165\u591a\u8bed\u8a00\u652f\u6301<\/p>\n res\/value\u4e2d\u662f\u9ed8\u8ba4\u7684\u5b57\u4f53<\/p>\n \u628a\u5b83\u590d\u5236\u4e00\u4efd\u6210\u4e3ares\/value-zh-rCN\u6587\u4ef6\u5939\uff0c\u7136\u540e\u628a\u5176\u4e2d\u6587\u5b57\u6539\u4e3a\u7b80\u4f53\u4e2d\u6587<\/p>\n 3. \u5728\u4e0d\u6539\u53d8\u7cfb\u7edf\u8bbe\u7f6e\u7684\u524d\u63d0\u524d\uff0c\u6d4b\u8bd5\u5f53\u524d\u5e94\u7528\u5bf9\u67d0\u4e00\u8bed\u8a00\u7684\u652f\u6301<\/p>\n import java.util.Local; \/\/ \u5f15\u5165\u76f8\u5173\u7c7b<\/p>\n import android.util.DisplayMetrics;<\/p>\n \u5728OnCreate\u51fd\u6570\u4e2d\u52a0\u5165\u4ee5\u4e0b\u672c\u5730\u5316\u8bbe\u7f6e<\/p>\n Resources res = getResources();<\/p>\n Configuration conf = res.getConfiguration();<\/p>\n conf.locale = Locale.SIMPLIFIED_CHINESE;\/\/ \u8bbe\u7f6e\u4e3a\u7b80\u4f53\u4e2d\u6587<\/p>\n DisplayMetrics dm = res.getDisplayMetrics();<\/p>\n res.updateConfiguration(conf,dm);<\/p>\n \u7f16\u8bd1\u540e\u518d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8bfb\u53d6\u7684\u5b57\u4e32\u5c31\u662fres\/value-zh-rCN\u76ee\u5f55\u4e2dstrings.xml\u91cc\u7684\u6587\u5b57\u4e86<\/p>\n<\/p>\n \u516d\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/strong><\/span><\/p>\n 1. android.app.Application<\/p>\n \u521b\u5efa\u4e00\u4e2a\u5c5e\u4e8e\u4f60\u81ea\u5df1\u7684android.app.Application\u7684\u5b50\u7c7b\uff0c\u7136\u540e\u5728 manifest\u4e2d\u7533\u660e\u4e00\u4e0b\u8fd9\u4e2a\u7c7b\uff0c\u8fd9\u662fandroid\u5c31\u4e3a\u6b64\u5efa\u7acb\u4e00\u4e2a\u5168\u5c40\u53ef\u7528\u7684\u5b9e\u4f8b\uff0c\u4f60\u53ef\u4ee5\u5728\u5176\u4ed6\u4efb\u4f55\u5730\u65b9\u4f7f\u7528 Context.getApplicationContext()\u65b9\u6cd5\u83b7\u53d6\u8fd9\u4e2a\u5b9e\u4f8b\uff0c\u8fdb\u800c\u83b7\u53d6\u5176\u4e2d\u7684\u72b6\u6001<\/p>\n 2. \u7528\u522b\u4eba\u5199\u7684java\u6587\u4ef6\u6539\u51fa\u81ea\u5df1\u7684\uff0cR.xx\u62a5\u9519<\/p>\n \u7531\u4e8e\u8d44\u6e90\u627e\u4e0d\u5bf9\uff0c\u8981\u628a\u5b83\u7684res\u6587\u4ef6\u548cAndroidManifest.xml\u5185\u5bb9\u4e5f\u505a\u76f8\u5e94\u66ff\u6362\uff0c\u6ce8\u610fxml\u91cc\u8fb9\u8981\u6539\u6210\u81ea\u5df1\u7684\u7c7b\u540d<\/p>\n 3. android\u4e2d\u53ef\u4ee5\u4f7f\u7528java\u4e2d\u7684\u7c7b\u5417<\/p>\n \u4e00\u822c\u6570\u636e\u7ed3\u6784\u4f7f\u7528\u5b83\uff0c\u800c\u754c\u9762\u4f7f\u7528android\u81ea\u5e26\u7684\u754c\u9762\u5143\u7d20<\/p>\n 4. layout\u4e2d\u7684\u63a7\u4ef6\u4e0e\u7a0b\u5e8f\u4e2d\u63a7\u4ef6\u7684\u5173\u7cfb<\/p>\n \u5e73\u7b49\u7684\uff0clayout\u4e2d\u7684\u63a7\u4ef6\u7528R.id.xxx\u5f97\u5230<\/p>\n 5. \u5e03\u5c40\u51b2\u7a81<\/p>\n requestWindowFeature\u4e0esetContentView\u51b2\u7a81<\/p>\n<\/p>\n \u4e03\u3001android\u4e2d\u51e0\u4e2a\u91cd\u8981\u7684\u5305\uff08\u5efa\u8bae\u9010\u4e2a\u719f\u6089\uff0c\u4f60\u5c31\u80fd\u719f\u80fd\u751f\u5de7\u4e86\uff01\uff09<\/span><\/strong><\/p>\n android.app \uff1a\u63d0\u4f9b\u9ad8\u5c42\u7684\u7a0b\u5e8f\u6a21\u578b\u3001\u63d0\u4f9b\u57fa\u672c\u7684\u8fd0\u884c\u73af\u5883<\/p>\n android.content \uff1a\u5305\u542b\u5404\u79cd\u7684\u5bf9\u8bbe\u5907\u4e0a\u7684\u6570\u636e\u8fdb\u884c\u8bbf\u95ee\u548c\u53d1\u5e03\u7684\u7c7b<\/p>\n android.database \uff1a\u901a\u8fc7\u5185\u5bb9\u63d0\u4f9b\u8005\u6d4f\u89c8\u548c\u64cd\u4f5c\u6570\u636e\u5e93<\/p>\n android.graphics \uff1a\u5e95\u5c42\u7684\u56fe\u5f62\u5e93\uff0c\u5305\u542b\u753b\u5e03\uff0c\u989c\u8272\u8fc7\u6ee4\uff0c\u70b9\uff0c\u77e9\u5f62\uff0c\u53ef\u4ee5\u5c06\u4ed6\u4eec\u76f4\u63a5\u7ed8\u5236\u5230\u5c4f\u5e55\u4e0a.<\/p>\n android.location \uff1a\u5b9a\u4f4d\u548c\u76f8\u5173\u670d\u52a1\u7684\u7c7b<\/p>\n android.media \uff1a\u63d0\u4f9b\u4e00\u4e9b\u7c7b\u7ba1\u7406\u591a\u79cd\u97f3\u9891\u3001\u89c6\u9891\u7684\u5a92\u4f53\u63a5\u53e3<\/p>\n android.net \uff1a\u63d0\u4f9b\u5e2e\u52a9\u7f51\u7edc\u8bbf\u95ee\u7684\u7c7b\uff0c\u8d85\u8fc7\u901a\u5e38\u7684java.net.* \u63a5\u53e3<\/p>\n android.os \uff1a\u63d0\u4f9b\u4e86\u7cfb\u7edf\u670d\u52a1\u3001\u6d88\u606f\u4f20\u8f93\u3001IPC \u673a\u5236<\/p>\n android.opengl \uff1a\u63d0\u4f9bOpenGL \u7684\u5de5\u5177<\/p>\n android.provider \uff1a\u63d0\u4f9b\u7c7b\u8bbf\u95eeAndroid \u7684\u5185\u5bb9\u63d0\u4f9b\u8005<\/p>\n android.telephony \uff1a\u63d0\u4f9b\u4e0e\u62e8\u6253\u7535\u8bdd\u76f8\u5173\u7684API \u4ea4\u4e92<\/p>\n android.view \uff1a\u63d0\u4f9b\u57fa\u7840\u7684\u7528\u6237\u754c\u9762\u63a5\u53e3\u6846\u67b6<\/p>\n android.util \uff1a\u6d89\u53ca\u5de5\u5177\u6027\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u65f6\u95f4\u65e5\u671f\u7684\u64cd\u4f5c<\/p>\n android.webkit \uff1a\u9ed8\u8ba4\u6d4f\u89c8\u5668\u64cd\u4f5c\u63a5\u53e3<\/p>\n android.widget \uff1a\u5305\u542b\u5404\u79cdUI \u5143\u7d20\uff08\u5927\u90e8\u5206\u662f\u53ef\u89c1\u7684\uff09\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u5c4f\u5e55\u4e2d\u4f7f\u7528<\/p>\n