#2. Re-using Layouts with include tag, merge tag
참고문서는 아래 공식문서를 참고하였다.
http://developer.android.com/intl/ja/training/improving-layouts/reusing-layouts.html
Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the<include/>and<merge/>tags to embed another layout inside the current layout.
안드로이는 작고 재사용가능한 상호작용을하는는 다양한 위젯들을 제공한다.
하지만 특별한 layout이나 큰 component를 재사용하는 것이 필요할지도 모른다.
효율적인 완성된 layout을 재사용하려면, <include/> tag 와 <merge/> tag 를 현재 layout안에 다른 layout을 끼워넣어서서 사용할 수 있다.
위 내용과 같이 include와 merge는 재사용과 좀더 간결한 xml code,
분할되어 깔끔한 구조를 위해 제공되는 tag이다.
include tag의 사용법을 알아보자.
아래 code는 우리가 재사용을 하고자하는 xml code이다.
titlebar.xml 이라고 name을 지어보자.
The root
View should be exactly how you'd like it to appear in each layout to which you add this layout.
root view (위 code에서는 FrameLayout)는 레이아웃을 나타내고 싶어하는 각 레이아웃에 정확하게 추가시켜야한다.
재사용을 하고자하는 레이아웃안에
<include layout="@layout/titlebar"/>
이런식으로 layout name을 써주면 된다.
You can also override all the layout parameters (anyandroid:layout_*attributes) of the included layout's root view by specifying them in the<include/>tag.
또한 include된 layout의 root view 의 layout parameters를 override 할 수 있다.
(android:layout_* attributes)
이번엔 merge tag에 대하여 알아보자.
merge tag는 사실 view hierarchy의 중복 viewgroup을 제거하는데에 도움이된다.
사용은 <merge xmlns:android="http://schemas.android.com/apk/res/android"></merge>
이렇게 사용하되 merge tag는 root view로만 사용이 가능하다.
내가 만약 위의 코드처럼 viewgroup없이 button두개만있는
레이아웃을 여러곳에서 재사용하고싶을 때
include tag를 사용하게되면 viewgroup으로 꼭 감싸야한다.
viewgroup은 굳이 필요없고 위젯만 필요한데 include를 사용하게되면
괜한 view 계층이 생겨버리는 것이다.
그럴때 그 불필요한 view group을 merge tag로 대체하면 끝!



댓글 없음:
댓글 쓰기