μΉ΄ν…Œκ³ λ¦¬ 보관물: Android

Android

XML λ ˆμ΄μ•„μ›ƒμ—μ„œ Android <merge> νƒœκ·Έμ˜ λͺ©μ μ€ λ¬΄μ—‡μž…λ‹ˆκΉŒ? 방법을 μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€. <Frame

νƒœκ·Έμ—μ„œ 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>

λ‹΅λ³€


이 글은 Android μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜λ˜μ—ˆκ³  λ‹˜μ— μ˜ν•΄ 에 μž‘μ„±λμŠ΅λ‹ˆλ‹€.