Toolbar中Overflow Menu不显示问题

参考谷歌官网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

有问题的欢迎留言。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值