{"id":461,"date":"2011-04-06T20:21:07","date_gmt":"2011-04-06T12:21:07","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=461"},"modified":"2011-04-10T07:19:04","modified_gmt":"2011-04-09T23:19:04","slug":"%e5%88%9b%e5%bb%batoast%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=461","title":{"rendered":"\u521b\u5efaToast\u901a\u77e5"},"content":{"rendered":"<p>toast\u901a\u77e5\u662f\u4e00\u79cd\u8df3\u51fa\u4e8e\u5c4f\u5e55\u8868\u9762\u7684\u6d88\u606f\u3002\u5b83\u53ea\u5360\u7528\u4fe1\u606f\u6240\u9700\u7684\u7a7a\u95f4\uff0c\u7528\u6237\u5f53\u524d\u7684\u6d3b\u52a8\u4ecd\u7136\u662f\u53ef\u89c1\u4e14\u80fd\u591f\u64cd\u4f5c\u7684\u3002\u8be5\u901a\u77e5\u81ea\u52a8\u5730\u6de1\u5165\u6de1\u51fa\uff0c\u4e0d\u63a5\u53d7\u4ea4\u4e92\u4e8b\u4ef6\u3002toast\u53ef\u4ee5\u7531\u540e\u53f0\u670d\u52a1\u521b\u5efa\uff0c\u56e0\u6b64\u5373\u4f7f\u7a0b\u5e8f\u672c\u8eab\u4e0d\u53ef\u89c1\uff0c\u4e5f\u80fd\u591f\u88ab\u663e\u793a\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u622a\u56fe\u5c55\u793a\u4e86\u95f9\u949f\u7a0b\u5e8f\u7684toast\u901a\u77e5\u793a\u4f8b\u3002\u4e00\u65e6\u95f9\u949f\u88ab\u6253\u5f00\u5c31\u4f1a\u663e\u793a\u4e00\u6761toast\u4ee5\u786e\u8ba4\u5176\u5df2\u7ecf\u6253\u5f00\u3002<a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/toast.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-462\" title=\"toast\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/toast.png\" alt=\"\" width=\"200\" height=\"140\" \/><\/a>toast\u53ef\u4ee5\u5728\u4e00\u4e2a\u6d3b\u52a8\u6216\u662f\u4e00\u4e2a\u670d\u52a1\u4e2d\u88ab\u521b\u5efa\u5e76\u663e\u793a\u3002\u5982\u679c\u662f\u4ece\u670d\u52a1\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2atoast\u901a\u77e5\uff0c\u5b83\u4f1a\u51fa\u73b0\u5728\u5f53\u524d\u5904\u4e8e\u7126\u70b9\u7684\u6d3b\u52a8\u4e4b\u524d\u3002<\/p>\n<p>\u5982\u679c\u5982\u8981\u7528\u6237\u5bf9\u901a\u77e5\u8fdb\u884c\u76f8\u5e94\u7684\u8bdd\uff0c\u8003\u8651\u4f7f\u7528\u72b6\u6001\u6761\u901a\u77e5\u3002<\/p>\n<h2>\u57fa\u672c\u5185\u5bb9<\/h2>\n<p>\u9996\u5148\uff0c\u7528\u67d0\u4e00makeText()\u65b9\u6cd5\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2aToast\u5bf9\u8c61\u3002\u8be5\u65b9\u6cd5\u9700\u8981\u4e09\u4e2a\u53c2\u6570\uff1a\u7a0b\u5e8fContext\uff0c\u6587\u672c\u4fe1\u606f\uff0c\u548ctoast\u7684\u6301\u7eed\u65f6\u95f4\u3002\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u6b63\u786e\u521d\u59cb\u5316\u7684Toast\u5bf9\u8c61\u3002\u53ef\u4ee5\u7528show()\u6765\u663e\u793a\u8be5toast\u901a\u77e5\uff0c\u5c31\u50cf\u4e0b\u9762\u7684\u8303\u4f8b\u90a3\u6837\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Context<\/span> context = getApplicationContext();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> text = <span style=\"color: #008000;\">\"Hello toast!\"<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">int<\/span> duration =<span style=\"color: #800080;\"> Toast<\/span>.LENGTH_SHORT;<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Toast<\/span> toast = <span style=\"color: #800080;\">Toast<\/span>.makeText(context, text, duration);<\/span>\r\n<span style=\"color: #000000;\">toast.show();<\/span><\/pre>\n<p>\u8be5\u8303\u4f8b\u6f14\u793a\u4e86\u5927\u90e8\u5206toast\u901a\u77e5\u6240\u9700\u7684\u5404\u9879\u5185\u5bb9\u3002\u901a\u5e38\u4e0d\u4f1a\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u4ee3\u7801\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u60f3\u8981\u5728\u5176\u4ed6\u4f4d\u7f6e\u663e\u793atoast\u6216\u662f\u8981\u7528\u81ea\u5df1\u7684\u5e03\u5c40\u66ff\u6362\u9ed8\u8ba4\u7684\u7b80\u5355\u6587\u5b57\u6d88\u606f\u7684\u8bdd\uff0c\u4e0b\u4e00\u8282\u4e2d\u5c06\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u3002<\/p>\n<p>\u8fd8\u53ef\u4ee5\u5c06\u65b9\u6cd5\u8fde\u63a5\u8d77\u6765\u4ee5\u907f\u514d\u6301\u6709Toast\u5bf9\u8c61\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Toast<\/span>.makeText(context, text, duration).show();<\/span><\/pre>\n<h2>\u5b9a\u4f4dToast\u901a\u77e5<\/h2>\n<p>\u6807\u51c6\u7684toast\u901a\u77e5\u51fa\u73b0\u5728\u5c4f\u5e55\u4e2d\u4e0b\u65b9\u3002\u53ef\u4ee5\u901a\u8fc7setGravity(int, int, tin)\u65b9\u6cd5\u6765\u6539\u53d8\u4f4d\u7f6e\u3002\u5b83\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u91cd\u529b\u5e38\u91cf\uff0cX\u65b9\u5411\u8865\u507f\u548cY\u65b9\u5411\u8865\u507f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u51b3\u5b9a\u628atoast\u7f6e\u4e8e\u5de6\u4e0a\u89d2\uff0c\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\u91cd\u529b\uff1a<\/p>\n<pre><span style=\"color: #000000;\">toast.setGravity(<span style=\"color: #800080;\">Gravity<\/span>.TOP|<span style=\"color: #800080;\">Gravity<\/span>.LEFT, <span style=\"color: #008080;\">0<\/span>, <span style=\"color: #008080;\">0<\/span>);<\/span><\/pre>\n<p>\u5982\u679c\u60f3\u8981\u628a\u4f4d\u7f6e\u63a8\u5230\u53f3\u4fa7\uff0c\u53ea\u9700\u589e\u52a0\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u503c\u3002\u8981\u5411\u4e0b\u79fb\u52a8\uff0c\u5c31\u589e\u52a0\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7684\u503c\u3002<\/p>\n<h2>\u521b\u5efa\u81ea\u5b9a\u4e49toast\u89c6\u56fe<\/h2>\n<p>\u5982\u679c\u4e0d\u6ee1\u8db3\u4e8e\u7b80\u5355\u7684\u6587\u672c\u6d88\u606f\uff0c\u8fd8\u53ef\u4ee5\u4e3atoast\u901a\u77e5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5e03\u5c40\u3002\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u5e03\u5c40\uff0c\u9700\u8981\u901a\u8fc7XML\u6216\u662f\u5728\u7a0b\u5e8f\u4ee3\u7801\u5185\u5b9a\u4e49\u4e00\u4e2aView\u5e03\u5c40\uff0c\u7136\u540e\u4f20\u9012\u6839View\u5bf9\u8c61\u81f3setView(View)\u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u7684XML\uff08\u4fdd\u5b58\u4e3atoast_layout.xml\uff09\u521b\u5efa\u51fa\u4e0b\u9762\u622a\u56fe\u4e2d\u7684\u5e03\u5c40\uff1a<\/p>\n<p><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/custom_toast.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-468\" title=\"custom_toast\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/custom_toast.png\" alt=\"\" width=\"200\" height=\"300\" \/><\/a><\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">&lt;LinearLayout<\/span> <span style=\"color: #800080;\">xmlns:android<\/span>=<span style=\"color: #008000;\">\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:id<\/span>=<span style=\"color: #008000;\">\"@+id\/toast_layout_root\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:orientation<\/span>=<span style=\"color: #008000;\">\"horizontal\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:padding<\/span>=<span style=\"color: #008000;\">\"10dp\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:background<\/span>=<span style=\"color: #008000;\">\"#DAAA\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &gt;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;ImageView<\/span> <span style=\"color: #800080;\">android:id<\/span>=<span style=\"color: #008000;\">\"@+id\/image\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0android:layout_width<\/span>=<span style=\"color: #008000;\">\"wrap_content\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0android:layout_marginRight<\/span>=<span style=\"color: #008000;\">\"10dp\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/&gt;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;TextView<\/span> <span style=\"color: #800080;\">android:id<\/span>=<span style=\"color: #008000;\">\"@+id\/text\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span>=<span style=\"color: #008000;\">\"wrap_content\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:textColor<\/span>=<span style=\"color: #008000;\">\"#FFF\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/&gt;<\/span>\r\n<span style=\"color: #000080;\">&lt;\/LinearLayout&gt;<\/span><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cLinearLayout\u5143\u7d20\u7684ID\u662f\u201ctoast_layout\u201d\u3002\u5fc5\u987b\u7528\u8fd9\u4e2aID\u4eceXML\u4e2d\u89e3\u538b\u51fa\u5e03\u5c40\u5185\u5bb9\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">LayoutInflater<\/span> inflater = getLayoutInflater();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">View<\/span> layout = inflater.inflate(R.layout.toast_layout,<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0(<span style=\"color: #800080;\">ViewGroup<\/span>) findViewById(R.id.toast_layout_root));<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">ImageView<\/span> image = (<span style=\"color: #800080;\">ImageView<\/span>) layout.findViewById(R.id.image);<\/span>\r\n<span style=\"color: #000000;\">image.setImageResource(R.drawable.android);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">TextView text<\/span> = (<span style=\"color: #800080;\">TextView<\/span>) layout.findViewById(R.id.text);<\/span>\r\n<span style=\"color: #000000;\">text.setText(<span style=\"color: #008000;\">\"Hello! This is a custom toast!\"<\/span>);<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Toast<\/span> toast = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Toast<\/span>(getApplicationContext());<\/span>\r\n<span style=\"color: #000000;\">toast.setGravity(<span style=\"color: #800080;\">Gravity<\/span>.CENTER_VERTICAL, <span style=\"color: #008080;\">0<\/span>, <span style=\"color: #008080;\">0<\/span>);<\/span>\r\n<span style=\"color: #000000;\">toast.setDuration(<span style=\"color: #800080;\">Toast<\/span>.LENGTH_LONG);<\/span>\r\n<span style=\"color: #000000;\">toast.setView(layout);<\/span>\r\n<span style=\"color: #000000;\">toast.show();<\/span><\/pre>\n<p>\u9996\u5148\uff0c\u7528getLayoutInflater()\uff08\u6216getSystemService()\uff09\u6765\u68c0\u7d22\uff0c\u4e4b\u540e\u4f7f\u7528inflate(int, ViewGroup)\u5c06\u5e03\u5c40\u5185\u5bb9\u4eceXML\u4e2d\u89e3\u538b\u3002\u5176\u4e2d\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5e03\u5c40\u8d44\u6e90ID\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6839View\u3002\u53ef\u4ee5\u5728\u88ab\u89e3\u538b\u7684\u5e03\u5c40\u4e2d\u627e\u5230\u5176\u4ed6\u7684View\u5bf9\u8c61\uff0c\u4e4b\u540e\u83b7\u53d6\u5e76\u5b9a\u4e49ImageView\u548cTextView\u5143\u7d20\u7684\u5185\u5bb9\u3002\u6700\u540e\uff0c\u7528Toast(Context)\u521b\u5efa\u4e00\u4e2a\u65b0\u7684toast\uff0c\u8bbe\u7f6e\u5176gravity\u548cduration\u7b49\u5c5e\u6027\u3002\u7136\u540e\u8c03\u7528setView(View)\u5c06\u5176\u4f20\u9012\u7ed9\u6240\u89e3\u538b\u7684\u5e03\u5c40\u3002\u73b0\u5728\u5c31\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528show()\u6765\u663e\u793a\u81ea\u5b9a\u4e49\u5e03\u5c40\u7684toast\u4e86\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u9664\u975e\u8981\u7528setView(View)\u6765\u5b9a\u4e49\u5e03\u5c40\uff0c\u4e0d\u7136\u4e0d\u8981\u4f7f\u7528\u516c\u6709\u6784\u9020\u51fd\u6570\u6765\u6784\u9020Toast\u3002\u5982\u679c\u6ca1\u6709\u53ef\u7528\u7684\u81ea\u5b9a\u4e49\u5e03\u5c40\uff0c\u5219\u5fc5\u987b\u8981\u7528makeText(Context, int, int)\u6765\u521b\u5efaToast\u3002<\/p>\n<p><a title=\"\u901a\u77e5\u7528\u6237\" href=\"http:\/\/leybreeze.com\/blog\/?p=452\">\u8fd4\u56de\u901a\u77e5\u7528\u6237<\/a><\/p>\n<p>\u672c\u4f5c\u54c1\u91c7\u7528<a title=\"\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae\" rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/\" target=\"_blank\">\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae<\/a>\u8fdb\u884c\u8bb8\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>toast\u901a\u77e5\u662f\u4e00\u79cd\u8df3\u51fa\u4e8e\u5c4f\u5e55\u8868\u9762\u7684\u6d88\u606f\u3002\u5b83\u53ea\u5360\u7528\u4fe1\u606f\u6240\u9700\u7684\u7a7a\u95f4\uff0c\u7528\u6237\u5f53\u524d\u7684\u6d3b\u52a8\u4ecd\u7136\u662f\u53ef\u89c1\u4e14\u80fd\u591f\u64cd\u4f5c\u7684\u3002\u8be5\u901a\u77e5 &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=461\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u521b\u5efaToast\u901a\u77e5\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[57],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/461"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=461"}],"version-history":[{"count":5,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":465,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions\/465"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}