안드로이드 <Kotlin>

[android] xmlns android, app 이해

TaeGyeong Lee 2023. 11. 22. 02:02

개요

액티비티 구현 시 루트 레이아웃에 다음과 같이 선언되어있음을 확인할 수 있습니다.

<androidx.constraintlayout.widget.ConstraintLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
  	...
    >

 

xmlns

xmls는 xml 네임스페이스를 정의합니다. 한 xml에서 다른 외부 xml에 정의된 변수를 활용하기 위해 사용합니다. xml 네임스페이스는 두 개 이상의 xml을 구별하여 충돌을 방지하는 변수명입니다.

 

xml 네임스페이스 정의 필요성

안드로이드에서 showAsAction 은 android와 app모두 정의되어 있을 수 있습니다.

android:showAsAction
app:showAsAction

이때, 어떠한 showAsAction을 사용할 것인지에 대해 명확한 정의가 필요합니다. 

굳이 android 가 아니어도 상관없습니다. androidCutsomNamespace 이런식으로 정의해도 됩니다. 말 그대로 변수명입니다.

 

xmlns:android

xmls:android = "http://schemas.android.com/apk/res/android" 임을 전제합니다.

android sdk 내부에 정의된 속성입니다. 해당 속성에 커서를 올린 후 Cntrl + b 하면 해당 속성이 정의된 파일을 볼 수 있습니다. 

android:layout_gravity="center"

 

... Android\Sdk\platforms\android-33\data\res\values 디렉토리에 있는 attr.xml 내부에 gravity 속성이 정의되어 있습니다.

attr.xml 파일 일부

<!-- Specifies how an object should position its content, on both the X and Y axes,
         within its own bounds.  -->
    <attr name="gravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />
        <!-- Push object to the left of its container, not changing its size. -->
        <flag name="left" value="0x03" />
        ...

 

xmlns:app

xmlns:app = "http://schemas.android.com/apk/res-auto" 임을 전제합니다.

자동으로 생성된 속성입니다. gradle/cache 디렉토리 하위에 생성되어 있습니다.

특정 라이브러리에서 가져와야 하는 경우 주로 사용할 수 있습니다.

app:layout_constraintEnd_toEndOf="parent"

 

values.xml 파일 일부

contraintlayout-2.1.4 라이브러리에서 제공하는 속성 layout_constraintBottom_toBottomOf 은 values.xml에 자동 생성되어 있음을 확인할 수 있습니다.

<attr format="reference|enum" name="layout_constraintBottom_toBottomOf">
        <enum name="parent" value="0"/>
    </attr>

 

참고 자료

 

XML namespace - Wikipedia

From Wikipedia, the free encyclopedia Method of providing unique elements and attributes in an XML document XML namespaces are used for providing uniquely named elements and attributes in an XML document. They are defined in a W3C recommendation.[1][2] An

en.wikipedia.org

 

도구 속성 참조  |  Android 스튜디오  |  Android Developers

Android 스튜디오에서 지원하는 도구 네임스페이스의 다양한 XML 속성은 디자인-시간 기능 또는 컴파일-시간 동작을 사용합니다.

developer.android.com

 

What is the 'app' Android XML namespace?

Here is an example of the app namespace that I've seen from a res/menu/main.xml file <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-

stackoverflow.com