Android Layout - when to use app: vs android:?

安稳与你 提交于 2019-11-30 05:15:39

You are talking about custom namespace.In android we can create custom views in additional to already available views. As per in Google developer docs.. To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:

Define custom attributes for your view in a resource element Specify values for the attributes in your XML layout Retrieve attribute values at runtime Apply the retrieved attribute values to your view

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]

So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.

Refer this

alkas

You can use the app namespace to have app compatibility with older API versions.

For example app:srcCompat="@drawable/customborder" has the same effects with android:background="@drawable/customborder" The difference is that the first will work correctly with older API's and the second will not display what you would like.

If you take a look at the beginning of the your layout xml files (in which you used app:) you will (probably) find lines like this:

<?xml version="1.0" encoding="utf-8"?>
<SOME_LAYOUT xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

in this case app: namespace will be used for custom attributes, specified by you inside attrs.xml file or by someone else in one of used libraries.

moreover you will find two variants

xmlns:app="http://schemas.android.com/apk/res-auto" 

xmlns:app="http://schemas.android.com/apk/[packagename]"

the difference between xmlns lines is res-auto take care of resolving our package as sometime we will add .debug or .test in our package and we already provided the packageid of the app Ex:

xmlns:app="http://schemas.android.com/apk/com.test.io.debug"
xmlns:app="http://schemas.android.com/apk/com.test.io.test"

Sometime the property with android only available in new Android version like

In this case, you should use app:... to make it work with older version.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!