日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Toolbar中Overflow Menu不显示问题

發(fā)布時(shí)間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Toolbar中Overflow Menu不显示问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考谷歌官網(wǎng)https://developer.android.com/training/appbar/setting-up.html#utility
google圖標(biāo)圖片下載地址:https://design.google.com/icons/
發(fā)現(xiàn)自己建立的Toolbar中沒(méi)有Overflow Menu,查看相關(guān)資料發(fā)現(xiàn),需要自己新建Menu布局,在Activity中通過(guò)onCreateOptionsMenu(Menu menu)自己加載它

配置文件

<application>android:theme="@style/Theme.AppCompat.Light.NoActionBar" </application>



Toolbar的布局
/res/layout/main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"><android.support.v7.widget.Toolbarandroid:id="@+id/my_toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:elevation="4dp"app:theme="@style/ThemeOverlay.AppCompat.ActionBar"app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar></RelativeLayout>



菜單項(xiàng)
/res/menu/toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/action_bar_menu"android:layout_width="match_parent"android:layout_height="match_parent"><item android:id ="@+id/search"android:icon="@drawable/ic_action_search"android:title="search"app:showAsAction="ifRoom" /><item android:id="@+id/copy"android:icon="@drawable/ic_action_copy"android:title="copy"app:showAsAction="ifRoom"/><item android:id="@+id/cut"android:title="cut"app:showAsAction="never"/></menu>

注意:app:showAsAction若為ifRoom則,作為app bar的按鈕,若為never則為overflow menu中的選項(xiàng),若app bar中的空間不足,則會(huì)將超出的部分全部放入overflow menu中

官方解釋:
【The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar. If you set app:showAsAction=”ifRoom” (as in the example code’s favorite action), the action is displayed as a button if there is room in the app bar for it; if there is not enough room, excess actions are sent to the overflow menu. If you set app:showAsAction=”never” (as in the example code’s settings action), the action is always listed in the overflow menu, not displayed in the app bar. 】



MainActivity.java

package com.app.bt;import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem;public class MainActivity extends AppCompatActivity{private Toolbar mToolbar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mToolbar = (Toolbar) findViewById(R.id.my_toolbar);mToolbar.setTitle("ToolbarTest");setSupportActionBar(mToolbar);mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {return false;}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main_activity_actions,menu);return super.onCreateOptionsMenu(menu);} }



參考文章
[1]http://blog.csdn.net/suppercoder/article/details/10212875
[2]http://blog.sina.com.cn/s/blog_4e1e357d0102ylri.html
[3]http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

有問(wèn)題的歡迎留言。

總結(jié)

以上是生活随笔為你收集整理的Toolbar中Overflow Menu不显示问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。