Android XML中的android:和app:前缀之间的区别


有什么

区别,更重要的是在Andriod view XML中使用不同前缀的必要性?

例如

<android.support.v7.widget.Toolbar
    android:id="@+id/actionToolBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentInsetEnd="20dp"
    app:contentInsetEnd="20dp"
    android:elevation="3dp"
  />

androidapp都有contentInsetEnd

android通常用于来自Android SDK本身的属性。

如果您使用的是支持库,则经常使用app

如果您使用的是自定义视图(您自己的视图或形成库),则还可能会看到其他命名空间。

以下是一些额外信息: http://developer.android.com/training/custom-views/create-view.html#customattr

应用程序命名空间用于自定义定义的属性,这些属性通常在 /values/attrs.xml 这是此类文件的示例

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SimpleTabIndicator">
        <attr name="numberOfTabs" format="integer"/>
        <attr name="indicatorColor" format="color"/>
    </declare-styleable>
</resources>

示例用法将是

<com.someapp.demo.SimpleTabIndicator
    android:id="@+id/tabIndicator"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#26292E"
    app:indicatorColor="#FFFDE992"
    app:numberOfTabs="5"/>

用于 Android 小部件和 UI 控件的 Android 命名空间。

app只是自定义视图的任何自定义参数的命名空间。

这可以是任何东西,但如果您看到根元素,则可能有一行xmlns:app="http://schemas.android.com/apk/res-auto"分配命名空间。

最新更新