在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题(原文:http://www.cnblogs.com/izhanjun/p/)
来源:互联网 发布:windows 体验指数7.3 编辑:程序博客网 时间:2024/05/17 04:47
今天很是苦恼,明明设置了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。
例如:
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 > 5 6 <item 7 android:id="@+id/action_search" 8 android:icon="@drawable/peasy" 9 app:showAsAction="always"10 android:title="@string/action_search"/>11 12 <!-- 设置, 在溢出菜单中展示 -->13 <item14 android:id="@+id/action_settings"15 android:showAsAction="never"16 android:title="@string/action_settings"/>17 <item18 android:id="@+id/action_about"19 app:showAsAction="never"20 android:title="@string/action_about"/>21 22 </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中显示的问题(原文:http://www.cnblogs.com/izhanjun/p/)
- 在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题...
- 在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题
- ActionBar中icon总是显示在overflow的问题
- android中actionbar的showAsAction属性设置为always无效解决方案
- 解决ActionBar中不显示item的问题,item显示在overflow中
- linux动态库加载的秘密(原文地址:http://www.cnblogs.com/LiuYanYGZ/p/5545835.html)
- android:showAsAction menu在xml中设置 以及对actionbar 的设置点击
- 正则表达式分类 区别 原文地址:http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html 则表达式:在计算机科学中,是指一个用来描述
- 关于js中数据类型的探讨,转载http://www.cnblogs.com/sharpxiajun/p/4133462.html
- 用递归在Dropdownlist中显示树状结构 http://www.cnblogs.com/singlepine/archive/2006/03/19/353453.html
- 浅谈栈区、堆区内存分配的区别(原文地址:http://www.cnblogs.com/houjun/p/4909413.html)
- mysql索引(原文地址包括所有mysql的知识:http://www.cnblogs.com/lyhabc/p/3776739.html)
- 在ASP.NET 2.0中开发通配符映射应用程序的一些问题 (转自:http://www.cnblogs.com/dudu/archive/2006/03/07/345107.html)
- 一些在JAVA中获得文件夹路径的方法http://www.cnblogs.com/lostyue/archive/2011/06/27/2091686.html
- 在做自动化测试之前你需要知道的,转自:http://www.cnblogs.com/fnng/p/3653793.html
- DPDK内存管理 原文地址:http://www.cnblogs.com/jintianfree/p/4018043.html
- Android: 详解触摸事件如何传递 - 玲音 原文 http://www.cnblogs.com/superlcr/p/3946034.html
- BZOJ 1031 [JSOI2007]字符加密Cipher
- linux兴趣小组暑假第六次讲座总结
- AndroidUI组件-LinearLayout 线性布局
- 【JavaScript】JS实现继承最常用的方法
- Activity中的onCreate()
- 在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题(原文:http://www.cnblogs.com/izhanjun/p/)
- Android Fragment 生命周期图
- 谷歌(Chrome)浏览器调试JavaScript小技巧
- 一个简单的makefile示例及其注释
- oracle11g使用PLSQL_Developer 连接报 ora-12546错误
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- 枚举值当数据字典使用
- 判断Edit控件Enabled状态的方法。
- java 泛型