{"id":736,"date":"2023-02-26T15:44:49","date_gmt":"2023-02-26T07:44:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=736"},"modified":"2023-04-29T17:08:29","modified_gmt":"2023-04-29T09:08:29","slug":"android-toolbar-custom-menu-2","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/android-toolbar-custom-menu-2\/","title":{"rendered":"Android Toolbar\u81ea\u5b9a\u4e49\u83dc\u5355"},"content":{"rendered":"<pre><code class=\"language-java\">Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);\ntoolbar.inflateMenu(R.menu.menu_main);  \/\/\u52a0\u8f7d\u83dc\u5355\u8d44\u6e90\ntoolbar.setOnMenuItemClickListener(listener);  \/\/\u83dc\u5355\u7684\u70b9\u51fb\u4e8b\u4ef6<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">private Toolbar.OnMenuItemClickListener listener = new Toolbar.OnMenuItemClickListener() {\n    @Override\n    public boolean onMenuItemClick(MenuItem item) {\n        switch (item.getItemId()) {\n            case R.id.action_settings:\n                startActivity(new Intent(MainActivity.this, NotCarouselActivity.class));\n                break;\n        }\n        return false;\n    }\n};<\/code><\/pre>\n<p>\u83dc\u5355\u8d44\u6e90\u6587\u4ef6\uff1a\/res\/menu\/menu_main.mxl<\/p>\n<pre><code class=\"language-xml\">&lt;menu xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    &gt;\n    &lt;item android:id=&quot;@+id\/action_edit&quot;\n        android:title=&quot;\u7f16\u8f91&quot;\n        android:orderInCategory=&quot;80&quot;\n        android:icon=&quot;@mipmap\/ab_edit&quot;\n        app:showAsAction=&quot;ifRoom&quot; \/&gt;\n\n    &lt;item android:id=&quot;@+id\/action_share&quot;\n        android:title=&quot;\u5206\u4eab&quot;\n        android:orderInCategory=&quot;90&quot;\n        android:icon=&quot;@mipmap\/ab_share&quot;\n        app:showAsAction=&quot;ifRoom&quot; \/&gt;\n\n    &lt;item android:id=&quot;@+id\/action_settings&quot;\n        android:title=&quot;\u975e\u5faa\u73af\u8f6e\u64ad&quot;\n        android:orderInCategory=&quot;100&quot;\n        app:showAsAction=&quot;never&quot;\/&gt;\n&lt;\/menu&gt;<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\u4e0d\u9700\u8981\u4f7f\u7528<code>setSupportActionBar(toolBar)<\/code>\uff0c\u5426\u5219\u83dc\u5355\u4e0d\u4f1a\u663e\u793a<\/p>\n<p>\u4f7f\u7528<code>setOnMenuItemClickListener<\/code>\u5904\u7406\u83dc\u5355\u70b9\u51fb\u4e8b\u4ef6\u4f1a\u8986\u76d6<code>onOptionsItemSelected<\/code>\u7684\u5904\u7406<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[62],"class_list":["post-736","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-toolbar"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/736\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}