天天看点

android开发action bar中menu菜单action overflow问题

http://blog.csdn.net/suppercoder/article/details/10212875

最近在学习action bar的内容,是直接参考android官网的资料学习。

      参考http://developer.android.com/training/basics/actionbar/adding-buttons.html 文档学习action bar的开发过程中,遇到一个问题,经过搜索以及查阅更详细文档,得到解答,现以记录。

      根据原文的描述,根据下面配置使用menu

res/menu/main_activity_actions.xml

应该得到动作栏的图标如图:

android开发action bar中menu菜单action overflow问题

注意,红框是我加上的强调。

问题所在:我根据文档配置后,设置有部分按钮为

附上我的配置:

android:showasaction="never"

[plain]

view plaincopyprint?

<menu xmlns:android="http://schemas.android.com/apk/res/android" >  

    <item android:id="@+id/action_search_id"  

          android:icon="@drawable/ic_action_search"  

          android:title="@string/action_search"  

          android:showasaction="ifroom" />  

    <item android:id="@+id/action_attach_id"  

          android:icon="@drawable/ic_action_attach"  

    <item android:id="@+id/action_call_id"  

          android:icon="@drawable/ic_action_call"  

          android:showasaction="never" />  

    <item android:id="@+id/action_copy_id"  

          android:icon="@drawable/ic_action_copy"  

</menu>  

配置上明显有两个是设置了never的显示属性,但出来的效果是没有红框所显示的action overflow样式。

android开发action bar中menu菜单action overflow问题

问题追踪:从搜索action overfolw的内容中找到一个很有价值的内容http://blog.sina.com.cn/s/blog_4ad7c2540101eh5c.html,按原文中提到:

4)action overflow  

action overflow中存放并不会频繁用到的操作。按照官方网页上的说法,“overflow图标仅显示在没有menu硬按键的手机上,而对于有menu键的手机,  

overflow图标是不显示的,当用户点击menu按键时弹出。”这样的说法比较蹊跷,似乎和google敦促手机厂商及软件开发商取消menu的行为不相匹配。  

我再参考官方文档http://developer.android.com/guide/topics/ui/actionbar.html 的确有如下描述:

the action bar provides users access to the most important action items relating to the app's current context. those that appear directly in the action bar with an icon and/or text are known as action buttons. actions that can't

fit in the action bar or aren't important enough are hidden in the action overflow. the user can reveal a list of the other actions by pressing the overflow button on the right side (or the device menu button, if available).

大意翻译如下:

很多程序需要提供与当前内容相关的动作选项作为跳转入口,动作栏正是给用户提供了这些入口的功能。通过各个动作按钮,动作栏直接呈现给用户的是一个图标或文字(也有可能是两者的组合)。而那些由于各种原因(有可能是空间不够,有可能是不太重要),不适合直接显示在动作栏上面的动作按钮,将会被放在隐藏动作按钮部分。用户可以通过点击隐藏动作按钮来显示被隐藏的其他按钮(如果设备有菜单功能键,显示隐藏动作的功能将会由menu菜单功能键实现)

注意到,最后一句很关键。我查找我开发的虚拟设备功能如图:

android开发action bar中menu菜单action overflow问题

可以看到,的确有menu功能键!这么来说,应该就解析的通了,显示隐藏动作的功能由菜单功能键实现。我们直接做一下验证,点击menu键,如图所示:

android开发action bar中menu菜单action overflow问题

在下方我们看到了显示的字符串,我原来的配置只简单的显示<string name="action_search">str_action_search</string>这里配置的字符串,对于演示没有多大的影响。

这本来不是什么大问题,但是仅仅一个小功能,也许也会使我们疑惑,在此解释清楚。

继续阅读