Android xmlns

XML Namespace

xmlns

xmlns是XML语言中表示Namespace位置的标签,被定义在元素的开始标签中时。
标准语法为:
<element xmlns:prefix="http://Namespace-name-URI">
其中分为两部分:

  • prefix:前缀
  • Namespace URI:所关联的Namespace位置

在Android xml布局文件头部的
xmlns:android="http://schemas.android.com/apk/res/android"
即Android API的Namespace。

xmlns:app

在引用Library的第三方View时,我们需要在XML布局文件头部添加
xmlns:app="http://schemas.android.com/apk/res-auto"
或者
xmlns:app="http://schemas.android.com/apk/res/包名"

在ADT 17.0.0(2012.3)更新中,添加了对Library自定义View的自定义attribute的支持。
通过使用http://schemas.android.com/apk/res-auto标识XML NameSpace,而不是以往的包名。

为什么要使用xmlns:app

在xml布局文件中,我们需要标识
xmlns:android="http://schemas.android.com/apk/res/android"
指定我们所用到的attribute。但由于API升级,有些新添加或者更新的attribute对低版本API无法支持或者效果不一致。
xmlns:app其实并不仅限于Library,而是针对整个App:无论是你引用的Library中的attribute,还是你自定义的全局attribute都有效。
因此,我我们引用的appcompat-v7Library使用和xmlns:android相同的自定义attribute(例如:android:showAsAction,添加于API11)。显然,使用android:showAsAction的话,低版本API设备是无法支持的,而使用app:showAsAction则能都支持所有API版本。
通过使用app:showAsAction,我们便使用到了appcompat-v7的自定义attribute,其定义在appcompat-v7res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    ...

    <!-- Base attributes that are available to all Item objects. -->
    <declare-styleable name="MenuItem">

        ...

        <!-- How this item should display in the Action Bar, if present. -->
        <attr name="showAsAction">
            <!-- Never show this item in an action bar, show it in the overflow menu instead.
                 Mutually exclusive with "ifRoom" and "always". -->
            <flag name="never" value="0" />
            <!-- Show this item in an action bar if there is room for it as determined
                 by the system. Favor this option over "always" where possible.
                 Mutually exclusive with "never" and "always". -->
            <flag name="ifRoom" value="1" />
            <!-- Always show this item in an actionbar, even if it would override
                 the system's limits of how much stuff to put there. This may make
                 your action bar look bad on some screens. In most cases you should
                 use "ifRoom" instead. Mutually exclusive with "ifRoom" and "never". -->
            <flag name="always" value="2" />
            <!-- When this item is shown as an action in the action bar, show a text
                 label with it even if it has an icon representation. -->
            <flag name="withText" value="4" />
            <!-- This item's action view collapses to a normal menu
                 item. When expanded, the action view takes over a
                 larger segment of its container. -->
            <flag name="collapseActionView" value="8" />
        </attr>

        ...

    </declare-styleable>
    
    ...

</resources>



作者:InSight_Suen
链接:https://www.jianshu.com/p/be7aa6dda558
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值