现在的位置: 首页 > 综合 > 正文

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

2013年10月16日 ⁄ 综合 ⁄ 共 2636字 ⁄ 字号 评论关闭

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

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

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

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

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

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

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

android:showAsAction="never"

附上我的配置:

<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"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <item android:id="@+id/action_call_id"
          android:icon="@drawable/ic_action_call"
          android:title="@string/action_search"
          android:showAsAction="never" />
    <item android:id="@+id/action_copy_id"
          android:icon="@drawable/ic_action_copy"
          android:title="@string/action_search"
          android:showAsAction="never" />
</menu>

配置上明显有两个是设置了never的显示属性,但出来的效果是没有红框所显示的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菜单功能键实现)

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

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

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

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

抱歉!评论已关闭.