νκ·Έμμ Romain Guyμ κ²μλ¬Ό μ μ½μ <merge />μ§λ§ μ¬μ ν μ μ©ν λ°©λ²μ μ΄ν΄νμ§ λͺ»ν©λλ€. <Frame />νκ·Έ μ μΌμ’
μ κ΅μ²΄μ
λκΉ , μλλ©΄ λ€μκ³Ό κ°μ΄ μ¬μ©λ©λκΉ?
<merge xmlns:android="....">
<LinearLayout ...>
.
.
.
</LinearLayout>
</merge>
λ€μ <include />λ€λ₯Έ νμΌμ μ½λ?
λ΅λ³
<merge/> λΆνμν ViewGroupμ μ κ±° ν μ μκΈ° λλ¬Έμ μ μ©ν©λλ€. μ¦, λ¨μν λ€λ₯Έ λ·°λ₯Ό κ°μΈκ³ λͺ©μ μ μ 곡νμ§ μλ λ μ΄μμμ
λλ€.
μλ₯Ό λ€μ΄, <include/>λ³ν©μ μ¬μ©νμ§ μκ³ λ€λ₯Έ νμΌμμ λ μ΄μμμ μ¬μ©νλ κ²½μ° λ νμΌμ λ€μκ³Ό κ°μ΅λλ€.
layout1.xml :
<FrameLayout>
<include layout="@layout/layout2"/>
</FrameLayout>
layout2.xml :
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
κΈ°λ₯μ μΌλ‘μ΄ λ¨μΌ λ μ΄μμκ³Ό κ°μ΅λλ€.
<FrameLayout>
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
</FrameLayout>
layout2.xmlμ ν΄λΉ FrameLayoutμ μ μ©νμ§ μμ μ μμ΅λλ€. <merge/>κ·Έκ²μ μ κ±°νλ λ° λμμ΄λ©λλ€. λ€μμ mergeλ₯Ό μ¬μ©νλ λͺ¨μ΅μ
λλ€ (layout1.xmlμ λ³κ²½λμ§ μμ).
layout2.xml :
<merge>
<TextView />
<TextView />
</merge>
μ΄κ²μ κΈ°λ₯μ μΌλ‘μ΄ λ μ΄μμκ³Ό λμΌν©λλ€.
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
νμ§λ§ μ¬μ© <include/>μ€μ΄λ―λ‘ λ€λ₯Έ κ³³μμ λ μ΄μμμ μ¬μ¬μ© ν μ μμ΅λλ€. FrameLayouts λ§ λ°κΎΈλ λ° μ¬μ©ν νμλ μμ΅λλ€.보기 λͺ¨μ / λμμ μ μ©ν κ²μ μΆκ°νμ§ μλ λ μ΄μμμ λ°κΎΈλ λ° μ¬μ©ν μ μμ΅λλ€.
λ΅λ³
ν¬ν¨ νκ·Έ
<include>νκ·Έλ₯Ό μ¬μ©νλ©΄ μ¬λ¬ νμΌλ‘ λ μ΄μμμ λλ μ μμ΅λλ€ : κ·Έκ²μ λ€λ£¨λ λ° λμμ΄ λ³΅μ‘ νκ±°λ λ무 κΈ΄ μ¬μ©μ μΈν°νμ΄μ€λ₯Ό μ 곡ν©λλ€.
λ€μκ³Ό κ°μ΄ λ κ°μ ν¬ν¨ νμΌμ μ¬μ©νμ¬ λ³΅μ‘ν λ μ΄μμμ λΆν νλ€κ³ κ°μ νκ² μ΅λλ€.
top_level_activity.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<include layout="@layout/include1.xml" />
<!-- Second include file -->
<include layout="@layout/include2.xml" />
</LinearLayout>
κ·Έλ° λ€μ include1.xmlλ° μ μμ±ν΄μΌν©λλ€ include2.xml.
ν¬ν¨ νμΌμ xmlμ λ λλ§ μκ°μ λ μ΄μμμ κ°λ¨ν λ€ν λ©λλ€ top_level_activity( #INCLUDEC μ λ§€ν¬λ‘ μ λ§€μ° μ μ¬ ν¨ ).
ν¬ν¨ νμΌμ μΌλ° μ μΈ λ μ΄μμ XMLμ λλ€.
include1.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:text="First include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
β¦ λ° include2.xml :
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:text="Button" />
보λ€? λ©μ§ κ²μ μμ΅λλ€. λ‘ Android λ€μ μ€νμ΄μ€λ₯Ό μ μΈν΄μΌν©λλ€ xmlns:android="http://schemas.android.com/apk/res/android.
λ°λΌμ top_level_activity.xml μ λ λλ§ λ²μ μ λ€μκ³Ό κ°μ΅λλ€.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<TextView
android:id="@+id/textView1"
android:text="First include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
μλ° μ½λ μμμ΄ λͺ¨λ κ²μ΄ ν¬λͺ
ν©λλ€. findViewById(R.id.textView1)νλ ν΄λμ€μμ μ¬λ°λ₯Έ μμ ―μ λ°νν©λλ€ (ν΄λΉ μμ ―μ΄ νλ λ μ΄μμκ³Ό λ€λ₯Έ XML νμΌλ‘ μ μΈ λ κ²½μ°μλ).
κ·Έλ¦¬κ³ μ²΄λ¦¬λ μμ μμ΅λλ€ : μκ°μ νΈμ§κΈ° λ 물건μ μμμΌλ‘ μ²λ¦¬ν©λλ€. μ΅μμ λ μ΄μμμ xmlμ΄ ν¬ν¨ λ μν λ‘ λ λλ§ λ©λλ€.
μλͺ¨κ° λκΊΌμμ§λ€
ν¬ν¨ νμΌμ ν΄λμ λ μ΄μμ xml νμΌμ΄λ―λ‘ μ΅μμ μμκ° νλ μμ΄μΌν©λλ€. λ°λΌμ νμΌμ λ μ΄μμ μμ ―μ΄ ν¬ν¨λμ΄μΌνλ κ²½μ° λ μ΄μμμ μ¬μ©ν΄μΌν©λλ€.
include1.xmlμ΄μ λ κ°μ§κ° μλ€κ³ κ°μ ν΄ λ΄
μλ€ TextView: λ μ΄μμμ΄ μ μΈλμ΄μΌν©λλ€. λ₯Ό μ νν©μλ€ LinearLayout.
include1.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
top_level_activity.xmlμ μΌλ‘ λ λλ§ λ κ²μ λλ€ :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
κ·Έλ¬λ λ λ 벨 LinearLayoutμ΄ μ€λ³΅λλ κ²μ κΈ°λ€λ¦¬μμμ€ !
μ€μ λ‘ λ κ°μ μ€μ²© μ μ νν λμΌν λ λλ§μ μν΄ LinearLayoutλ κ°μ TextViewν¬ν¨ λ μ μμΌλ―λ‘ λͺ©μ μ΄ μμ΅λλ€ .layout1
κ·Έλμ μ°λ¦¬κ° λ ν μ μμ§?
λ³ν© νκ·Έλ₯Ό μ λ ₯νμμμ€
<merge>νκ·Έλ μ€λ³΅ λ¬Έμ μμ΄ μ’
λ₯λ₯Ό λ€λ£¨λ μ΅μμ μμ€ μμλ₯Ό μ 곡ν©λλ€ λ¨μ§ λλ―Έ νκ·Έμ
λλ€.
μ΄μ include1.xml μ λ€μκ³Ό κ°μ΅λλ€.
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</merge>
μ΄μ top_level_activity.xml μ λ€μ κ³Ό κ°μ΄ λ λλ§λ©λλ€.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
νλμ κ³μΈ΅ ꡬ쑰 μμ€μ μ μ₯νκ³ μΈλͺ¨μλ보기λ₯Ό νΌνμμμ€. Romain Guyλ μ΄λ―Έ λ μ μλ©΄ν©λλ€.
μ§κΈ λ ν볡νμ§ μμ΅λκΉ?
λ΅λ³
blazeroniλ μ΄λ―Έ κ·Έκ²μ λͺ ννκ² λ§λ€μμ΅λλ€. λͺ μ λ§ μΆκ°νκ³ μΆμ΅λλ€.
<merge>λ μ΄μμ μ΅μ νμ μ¬μ©λλ©° λΆνμν μ€μ²©μ μ€μ΄λ λ° μ¬μ©λ©λλ€.<merge>νκ·Έκ° ν¬ν¨ λ λ μ΄μμμ΄ λ€λ₯Έ λ μ΄μμμ μΆκ°λλ©΄<merge>λ Έλκ° μ κ±°λκ³ ν΄λΉ μμ λ·°κ° μ μμμ μ§μ μΆκ°λ©λλ€.
λ΅λ³
λ¬΄μ¨ μΌμ΄ μΌμ΄λκ³ μλμ§ λ κΉμ΄ μκΈ° μν΄ λ€μ μμ λ₯Ό λ§λ€μμ΅λλ€. activity_main.xml λ° content_profile.xml νμΌμ μ΄ν΄λ³΄μμμ€ .
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/content_profile" />
</LinearLayout>
content_profile.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Howdy" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hi there" />
</LinearLayout>
μ¬κΈ°μμ ν½μ°½νμ λμ μ 체 λ μ΄μμ νμΌμ λ€μκ³Ό κ°μ΅λλ€.
<LinearLayout>
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
</LinearLayout>
λΆλͺ¨ LinearLayout λ΄λΆμ LinearLayoutμ΄μμ΄ μ΄λ€ μ©λλ‘λ μ¬μ©λμ§ μμΌλ©° μ€λ³΅λλ κ²μ νμΈνμμμ€. Layout Inspector λꡬλ₯Ό ν΅ν λ μ΄μμμ 보면μ΄λ₯Ό λͺ ννκ² μ€λͺ ν μ μμ΅λλ€.
LinearLayoutκ³Ό κ°μ ViewGroup λμ mergeλ₯Ό μ¬μ©νλλ‘ μ½λλ₯Ό μ λ°μ΄νΈ ν ν content_profile.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Howdy" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hi there" />
</merge>
μ΄μ λ μ΄μμμ λ€μκ³Ό κ°μ΅λλ€
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
μ¬κΈ°μ μ€λ³΅ LinearLayout ViewGroupμ΄ μ κ±°λμμμ μ μ μμ΅λλ€. μ΄μ Layout Inspector λꡬλ λ€μκ³Ό κ°μ λ μ΄μμ κ³μΈ΅μ μ 곡ν©λλ€.
λ°λΌμ λΆλͺ¨ λ μ΄μμμ΄ μμ λ μ΄μμμ λ°°μΉ ν μμμ λ νμ λ³ν© μ μ¬μ© νκ±°λ κ³μΈ΅ ꡬ쑰μ μ€λ³΅ λ·° κ·Έλ£Ήμ΄ μμμ μ΄ν΄νλ©΄ λ³ν©μ λ³΄λ€ μ ννκ² μ¬μ© νμμμ€.
λ΅λ³
λ³ν©μ μ¬μ©ν΄μΌνλ λ λ€λ₯Έ μ΄μ λ ListView λλ GridViewμμ μ¬μ©μ μ μ보기 κ·Έλ£Ήμ μ¬μ©ν λμ λλ€. λͺ©λ‘ μ΄λν°μμ viewHolder ν¨ν΄μ μ¬μ©νλ λμ μ¬μ©μ μ μ보기λ₯Ό μ¬μ©ν μ μμ΅λλ€. μ¬μ©μ μ μ보기λ 루νΈκ° λ³ν© νκ·Έ μΈ XMLμ λΆν립λλ€. μ΄λν° μ½λ :
public class GridViewAdapter extends BaseAdapter {
// ... typical Adapter class methods
@Override
public View getView(int position, View convertView, ViewGroup parent) {
WallpaperView wallpaperView;
if (convertView == null)
wallpaperView = new WallpaperView(activity);
else
wallpaperView = (WallpaperView) convertView;
wallpaperView.loadWallpaper(wallpapers.get(position), imageWidth);
return wallpaperView;
}
}
λ€μμ λ§μΆ€λ³΄κΈ° κ·Έλ£Ήμ λλ€.
public class WallpaperView extends RelativeLayout {
public WallpaperView(Context context) {
super(context);
init(context);
}
// ... typical constructors
private void init(Context context) {
View.inflate(context, R.layout.wallpaper_item, this);
imageLoader = AppController.getInstance().getImageLoader();
imagePlaceHolder = (ImageView) findViewById(R.id.imgLoader2);
thumbnail = (NetworkImageView) findViewById(R.id.thumbnail2);
thumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
public void loadWallpaper(Wallpaper wallpaper, int imageWidth) {
// ...some logic that sets the views
}
}
κ·Έλ¦¬κ³ μ¬κΈ° XMLμ΄ μμ΅λλ€ :
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/imgLoader"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:src="@drawable/ico_loader" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>