Android XML中android:和app:前缀的区别是什么?

50

在Andriod视图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


这是一个有用的答案:https://dev59.com/dV8d5IYBdhLWcg3wkSwV - Elisey Rodriguez Moraga
3个回答

48

谢谢。但是我不理解一个特殊情况,当我们在cardview中使用一个小部件时,约束布局属性在app命名空间(例如app:layout_constraintStart_toStartOf)和card_view命名空间(例如card_view:layout_constraintStart_toStartOf)之间有什么区别? - Reyhane Farshbaf

14

app 命名空间用于自定义属性,这些属性通常在 /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命名空间用于Android的小部件和用户界面控件。


11

app是自定义视图(custom View)的任何自定义参数的命名空间。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接