生活随笔
收集整理的這篇文章主要介紹了
Toolbar中Overflow Menu不显示问题
小編覺得挺不錯(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中沒有Overflow Menu,查看相關(guān)資料發(fā)現(xiàn),需要自己新建Menu布局,在Activity中通過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 .v 7
.widget .Toolbar android: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 .v 7
.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中的空間不足,則會將超出的部分全部放入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;
@Override protected 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() {
@Override public boolean onMenuItemClick (MenuItem item) {
return false ;}});}
@Override public 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
有問題的歡迎留言。
總結(jié)
以上是生活随笔 為你收集整理的Toolbar中Overflow Menu不显示问题 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。