【问题标题】:Android use namespace as prefix for attributes in libraryAndroid 使用命名空间作为库中属性的前缀
【发布时间】:2013-08-29 14:53:40
【问题描述】:

我正在尝试通过支持库 v7 来实现 ActionBar。因为我想支持Android 2.1(API 7)及以上的应用。

我在http://developer.android.com/guide/topics/ui/actionbar.html 中读到以下内容: "使用支持库中的 XML 属性 请注意,上面的 showAsAction 属性使用标签中定义的自定义命名空间。这在使用支持库定义的任何 XML 属性时是必需的,因为这些属性在旧设备上的 Android 框架中不存在。因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。”

我的问题是如何使用我自己的命名空间作为支持库定义的所有属性的前缀?

请逐步解释! //谢谢。

【问题讨论】:

    标签: android namespaces prefix


    【解决方案1】:

    这里是一个菜单示例:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:whatever="http://schemas.android.com/apk/res-auto">
    
        <item
            android:id="@id/wims_ab_done"
            android:icon="@drawable/white_check_mark"
            android:title="@string/save"
            whatever:showAsAction="always"/>
        <item
            android:id="@id/wims_ab_items_remove"
            android:icon="@drawable/wims_remove"        
            android:title="@string/remove"
            whatever:showAsAction="always"/>
    
    </menu>
    

    整个理念是在根menu 的命名空间属性中拥有http://schemas.android.com/apk/res-auto。命名空间标识符是什么并不重要,只要它是有效的并且在使用 showAsAction 属性时按原样使用即可。这能回答你的问题吗?

    【讨论】:

    • 在这种特定情况下,当我使用“schemas.android.com/apk/res-auto”时会发生什么?与 3.0 和 abow 内置的 showAsAction 相比,它如何补充。感谢您的快速回答! :)
    • 简而言之:这是一个避免命名空间冲突的问题,兼容性包确保您不会碰到。
    • 我收到错误:Gradle: No resource identifier found for attribute 'showAsAction' in package 'com.example.example',我错过了什么?
    • 不幸的是,我没有使用 Gradle,而是使用旧的 Eclipse ADT。除了谷歌搜索:"Gradle": No resource identifier found for attribute 'showAsAction'我无法帮助你。
    • @gunar 是否可以进行设置以使支持库中定义的自定义属性不需要任何前缀?还是必须以任何方式定义 xmlns?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2012-05-27
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多