Toolbar中Overflow Menu不显示问题

来源:互联网 发布:js中template 怎么用 编辑:程序博客网 时间:2024/05/22 00:42

参考谷歌官网https://developer.android.com/training/appbar/setting-up.html#utility
google图标图片下载地址:https://design.google.com/icons/
发现自己建立的Toolbar中没有Overflow Menu,查看相关资料发现,需要自己新建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.v7.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.v7.widget.Toolbar></RelativeLayout>



菜单项
/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中的选项,若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

有问题的欢迎留言。

1 0