在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题
今天很是苦恼,明明设置了android:showAsAction="always",但是所有的items全部都显示在overflow中,然后在官网发现了答案。
如果你为了兼容 Android 2.1 的版本使用了 Support 库,在 android 命名空间下showAsAction 属性是不可用的。Support 库会提供替代它的属性,你必须声明自己的 XML 命名空间,并且使用该命名空间作为属性前缀。(一个自定义 XML 命名空间需要以你的 app 名称为基础,但是可以取任何你想要的名称,它的作用域仅仅在你声明的文件之内。)
添加此命名空间 xmlns:app="http://schemas.android.com/apk/res-auto" ,使用app:showAsAction代替android:showAsAction。
例如:
<?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"
> <item
android:id="@+id/action_search"
android:icon="@drawable/peasy"
app:showAsAction="always"
android:title="@string/action_search"/> <!-- 设置, 在溢出菜单中展示 -->
<item
android:id="@+id/action_settings"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_about"
app:showAsAction="never"
android:title="@string/action_about"/> </menu>
UI依然很丑,但是效果实现了,大家将就着看吧。

另外,意外发现在actionBar只显示了icon但是没有显示title,这是怎么回事呢?于是又在官网深挖了....
看到了这一段... https://developer.android.com/guide/topics/ui/actionbar.html#Adding
If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText" to the showAsAction attribute. For example: <item yourapp:showAsAction="ifRoom|withText" ... />
Note: The "withText" value is a hint to the action bar that the text title should appear. The action bar will show the title when possible, but might not if an icon is available and the action bar is constrained for space. You should always define the title for each item even if you don't declare that the title appear with the action item, for the following reasons: If there's not enough room in the action bar for the action item, the menu item appears in the overflow where only the title appears.
Screen readers for sight-impaired users read the menu item's title.
If the action item appears with only the icon, a user can long-press the item to reveal a tool-tip that displays the action title.
The icon is optional, but recommended. For icon design recommendations, see the Iconography design guide. You can also download a set of standard action bar icons (such as for Search or Discard) from the Downloads page. You can also use "always" to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room. However, it might be necessary to use this value if the item includes an action view that cannot be collapsed and must always be visible to provide access to a critical feature.
简而言之,如果同时设置了icon和title,默认只会显示icon。
如果想同时显示title和icon,可以加入app:showAsAction="always|withText",但是即便这样也不会一定生效,withText对actionBar的title来说只是一个hint,在条件允许的情况下actionBar会显示title,但是当设置了icon并由于空间限制也不会显示title。
不过,官方还是建议我们设置title的,在长按icon的情况下title就会出现,另外官方还建议showAsAction最好设置为ifRoom,如果设置为always可能会在比较窄的屏幕上带来布局的问题。
在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题的更多相关文章
- 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体
[源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...
- IntelliJ IDEA中如何设置同时打开多个文件且分行显示?
Window→Editor Tabs→Tabs Placement→Show Tabs in Single Row 取消选中后即可在多行显示 下图为实际显示效果: 还可以自行设置打开文件窗口数(默认 ...
- ios系统中各种设置项的url链接
ios系统中各种设置项的url链接 在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplic ...
- SSIS变量属性中EvaluateAsExpression设置的作用
我们在做SqlServer SSIS包开发的时候,经常会用到SSIS的变量,我们可以使用和修改SSIS变量的值使得SSIS包的逻辑更灵活,如下图所示: 在定义SSIS变量的时候可以使用固定值(如上图中 ...
- myeclipse中UTF-8设置
myeclipse中UTF-8设置 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace ...
- Android 中布局设置导致的TextView不显示的问题
将TextView放入TableLayout中,设置TextView的Layout Witdh/Layout Height 为Wrap Content或其他大小,导致TextView内容无法显示,改为 ...
- Android中如何设置RadioButton在文字的右边,图标在左边
from:http://blog.csdn.net/sunnyfans/article/details/7901592?utm_source=tuicool&utm_medium=referr ...
- MVC中如何设置路由指定默认页
MVC中怎么设置默认页,在webform中 只要右键设置起始页就可以,但MVC中却没有这个功能,其实MVC更简单 如下: Login是控制器,Index 是动作 在全局Global.asax中改动下即 ...
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户 ...
随机推荐
- python调用Linux下so文件
1.通过C语言编写一个简单max函数,生成一个max.so链接库 /* * # -shared 为链接库 让编译器知道是要编译一个共享库 * # -fPIC(Position Independent ...
- Sharepoint2013商务智能学习笔记之使用Current User Filter筛选Excel 数据(六)
Sharepoint自带的filter可以和Excel Web Access互动,下面将制作一个Demo,使用Current User Filter根据当前登录用户自动筛选Excel. 第一步,用Ex ...
- Oracle 11g 、 Oracle 11g select 、 PLSQL 、 Sql Server迁移助手(SSMA)6.0/7.1 网盘下载地址
- - - - - - - - 链接: https://pan.baidu.com/s/1q-uwAfeLOPxzBBx6V1pYLg 提取码: hei9
- 洛谷P3006 [USACO11JAN]瓶颈Bottleneck(堆模拟)
传送门 感觉这题的思路还是挺不错的.然而为啥全网就一个题解而且只有代码……然后我只好看着代码理解了好久…… 题意就是有一棵树,每一个节点向他父亲节点连边,且有一个容量表示每一秒可以经过的牛的数量,每一 ...
- bzoj2502: 清理雪道(有源汇有上下界最小流)
传送门 别说话,自己看,我不会->这里 我这里用的建图方法是先跑一次最大流,连上$(t,s,inf)$之后再跑一遍,然后答案就是之前连的那条边的反向边的流量 据说还有种方法是连上$(t,s,in ...
- HTML基本标签问题总结
一.常见的内联元素和块元素 块级元素: div, form, (列表相关ul,dl,ol,li,) h1-6,p,table 内联元素: a, br, u,(b , em, strong, i,) i ...
- css奇技淫巧—border-radius
官方介绍: 浏览器支持:IE9+, Firefox 4+, Chrome, Safari 5+,和Opera支持border-radius属性. border-radius 属性是一个最多可指定四个 ...
- emmet中的用法
CSS Abbreviations Link VALUES LINK Emmet is about more than just HTML elements. You can inject value ...
- 自定义滚动条插件 mCustomScrollbar 使用介绍
引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...
- 转 nagios监控oracle运行状况
https://blog.csdn.net/heizistudio/article/details/8638488 nrpe安装脚本 nagios-plugins-1.4.13.tar.gznrpe- ...