본문 바로가기
프로그래밍/안드로이드 <Kotlin>

[Android] 안드로이드 android:tag android:name android:id 차이

by TaeGyeong Lee 2024. 9. 14.

개요 

안드로이드 개발 시 사용할 수 있는 속성 android:tag android:name android:id 세 가지의 차이를 알아보겠습니다. 속성 별 사용 권장되는 상황이 다릅니다. 

 

android:id

액티비티 메소드에서 활용합니다.  

<Button
   android:id="@+id/my_button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/my_button_text"/>
Button myButton = (Button) findViewById(R.id.my_button);

 

android:tag

id를 보조하거나, 가변적인 객체 리스트에서 활용합니다. 일반적인 상황에선 id사용을 권장합니다. 

 

android:name 

Fragment의 고유 명으로 사용합니다. 

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.ExampleFragment" />

아래와 같이 Fragment 교체 및 트랜잭션 시 활용합니다. 

// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment_container, ExampleFragment.class, null);

 

name 속성을 통해서만 Fragment 교체 및 트랜잭션을 통제할 수 있는 건 아닙니다. class 속성을 사용하여 Fragment 교체 및 트랜젝션 구현이 가능합니다. 

<androidx.fragment.app.FragmentContainerView
    class="com.example.ExampleFragment"
    ... />

 

참고 자료 

 

What are "tag" and "id" on Layouts?

I know how the switch statement works but I don't know what this means (R.id.webbutton). Can anyone please explain what it is and also what is TAG? Is there any guide for the beginners? I mean abs...

stackoverflow.com

 

프래그먼트 만들기  |  Android Developers

프래그먼트 만들기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 프래그먼트는 활동 내에서 사용자 인터페이스의 모듈식 부분을 말합니다. 프래그먼트는

developer.android.com

 

Activity Layout: Fragment class: vs android:name attributes

I've read the documentation about Fragments in the Android Developer Guide and I've seen that sometimes they specify the class to instantiate with the Fragment tag attribute android:name and sometime

stackoverflow.com