¾È±Ô °øºÎ¹æ

Android > resource - xml ¸®¼Ò½º

µî·ÏÀÏ : 2017-07-05 17:04 Á¶È¸¼ö : 53,163

xml ÆÄÀϵ鵵 ¸®¼Ò½º¿¡ ÀÛ¼ºÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù.

»ç¿ëÇÒ xml ÆÄÀÏÀº /res/ ¹Ø¿¡ xml Æú´õ¸¦ »ý¼ºÇÏ¿© /res/xml Æú´õ¿¡ ³Ö¾î¾ß ÇÑ´Ù.

xml Æú´õ¸¦ ¸¸µå·Î xml ÆÄÀÏÀ» Çϳª ¸¸µé¾î¼­ È£ÃâÇغ¸ÀÚ.

1. res(¸®¼Ò½º)¿¡ xml ÆÄÀÏ ÀÛ¼ºÇϱâ

/res/ ¹Ø¿¡ xml Æú´õ¸¦ ¸¸µé°í ±× ¾Æ·¡¿¡ xml ÆÄÀÏÀ» ÀÛ¼ºÇÑ´Ù.

¿©±â¼­´Â custom_list.xml À̶ó´Â ÆÄÀÏÀ» ÀÛ¼ºÇÏ°Ú´Ù.

custom_list.xml ÀÛ¼º
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

		<listview android:id="@+id/ListView1" android:layout_height="wrap_content" android:layout_width="fill_parent">

	</listview>
</relativelayout>


- ¸¦ ¼±¾ðÇÏ°í ±× ¾Æ·¡¿¡ ű׵éÀ» ÀÛ¼ºÇÏ¿´´Ù.

2. xmlÀ» º¸¿©ÁÙ ÀÛ¼º

- ÀÛ¼ºÇÑ custom_list.xml À» Àоî¿Í ÆĽÌÇÑ ÈÄ, È­¸é¿¡ ÇüÅ·Πº¸¿©ÁÙ °ÍÀÌ´Ù.
- È­¸é¿¡ º¸¿©ÁÙ ¸¦ Çϳª ÀÛ¼ºÇÑ´Ù.
 
		<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
		xmlns:tools="http://schemas.android.com/tools"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:paddingBottom="@dimen/activity_vertical_margin"
		android:paddingLeft="@dimen/activity_horizontal_margin"
		android:paddingRight="@dimen/activity_horizontal_margin"
		android:paddingTop="@dimen/activity_vertical_margin"
		tools:context=".MainActivity" >

		<ListView
			android:id="@+id/ListView1"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			/>

	</RelativeLayout>


- id : ListView1 ÀÎ ¸¦ Çϳª ÀÛ¼ºÇß´Ù.
- ÇöÀç ÀÌ ¿¡´Â º¸¿©ÁÖ´Â µ¥ÀÌÅÍ°¡ ¾ø´Ù.

3. Activity.java ¿¡¼­ xml À» °¡Á®¿Í ÆĽÌÇÏ¿© È­¸é¿¡ List ·Î º¸¿©ÁÖ±â 
 
	public class MainActivity extends Activity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
			// xmlÀ» ÆĽÌÇÏ¿© ÀúÀåÇÒ ArrayList¸¦ ¼±¾ðÇÑ´Ù.
			ArrayList items = new ArrayList();
				
			try{
				// custom_list.xml À» °¡Á®¿Í XmlPullParser ¿¡ ³Ö´Â´Ù.
				XmlPullParser customList = getResources().getXml(R.xml.custom_list);
					
				// ÆĽÌÇÑ xml ÀÌ END_DOCUMENT(Á¾·á ű×)°¡ ³ª¿Ã¶§ ±îÁö ¹Ýº¹ÇÑ´Ù.
				while(customList.getEventType()!=XmlPullParser.END_DOCUMENT) {
					// ű×ÀÇ Ã¹¹ø° ¼Ó¼ºÀÏ ¶§,
					if(customList.getEventType()==XmlPullParser.START_TAG)	{
						// À̸§ÀÌ "custom" À϶§, ù¹ø° ¼Ó¼º°ªÀ» ArrayList¿¡ ÀúÀå
						if(customList.getName().equals("custom"))	{
							items.add(customList.getAttributeValue(0));
						}
						
					}

				  // ´ÙÀ½ ű׷ΠÀ̵¿
				  customList.next();
				}
			}catch(XmlPullParserException e){
			   e.printStackTrace();
			}catch(IOException e){
			   e.printStackTrace();
			}
				
			// ArrayAdapter¿¡ ArrayList¸¦ ³Ö¾î¼­ ListView¿¡ µî·ÏÇÑ´Ù.
			ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
				
			ListView listView1 = (ListView)findViewById(R.id.ListView1);
			listView1.setAdapter(adapter);
		}
	}


- getResource().getXml() À» »ç¿ëÇÏ¿© ¸®¼Ò½º¿¡¼­ xmlÀ» °¡Á®¿Í¼­ XmlPullParser¿¡ ´ëÀÔÇÑ´Ù.
- ÆÄ½ÌµÈ xmlÀ» Á¾·á űװ¡ ³ª¿Ã¶§ ±îÁö while()¿¡¼­ µ¹¸é¼­, ù¹ø° ¼Ó¼º°ªÀ» ÀúÀåÇÑ´Ù.
- ÀúÀåÇÑ °ªµéÀ» ¿¡ µî·ÏÇÏ¿© Ãâ·ÂÇÑ´Ù. ½ÇÇàÈ­¸éÀ» º¸ÀÚ!



- ÀÛ¼ºÇÑ custom_list.xml ÀÇ ³»¿ëÀ» ÆĽÌÇÏ¿© ¿¡ Ãâ·ÂÇß´Ù.
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.