Android/Development Tips

커스텀뷰와 merge 태그

Nanamare 2018. 12. 12. 23:17
728x90

커스텀 뷰를 만들때 상속받아 뷰와 커스텀 뷰를 포함하는 뷰 그룹과 불필요한 중첩을 피하기 위해 merge 태그를 사용한다.


가령 예를 들어 


<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">

<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</merge>

이런 식으로 사용하면 된다.


merge 대신 LinearLayout을 사용할 수 있지만, 저 xml을 사용하는 커스텀 뷰가 LinearLayout을 상속받아 만든 것이라면 LinearLayout 안에 또 LinearLayout이 존재하는데 이 경우 중복 되기 때문에 merge를 사용하면 중첩을 피할수 있다.




728x90