¾È±Ô °øºÎ¹æ

Android > Android XML Selector »ý¼º ¹× Àû¿ëÇϱâ

µî·ÏÀÏ : 2017-07-05 18:40 Á¶È¸¼ö : 53,415

¾Èµå·ÎÀ̵忡¼­ Selector¸¦ xml·Î ¸¸µé¾î¼­ ButtonÀ̳ª ListView µî Ŭ¸¯ À̺¥Æ®°¡ µé¾î°¡´Â View¿¡ Àû¿ëÇÒ ¼ö ÀÖ´Ù.

±âº»À¸·Î Á¦°øÇÏ´Â View¸¦ Ưº°È÷ Ä¿½ºÅÒÇÏ°Ô »ç¿ëÇÏÁö ¾Ê´Â ÀÌ»ó ±âº» Á¦°øÇÏ´Â Selector±â´ÉÀ» »ç¿ëÇÒ ¼ö ÀÖÁö¸¸,

View¸¦ Ä¿½ºÅÒÇÏ°Ô »ç¿ëÇÏ´Â °æ¿ì°¡ ´ëºÎºÐÀ̱⠶§¹®¿¡ Selector¸¦ »ç¿ëÇؾßÇÑ´Ù.

¿¹¸¦ µé¸é ButtonÀ» ´­·¶À»¶§(pressed)¿Í ´©¸£Áö ¾Ê¾ÒÀ» °æ¿ì(nonPressed)¿¡ À̹ÌÁö, ¶Ç´Â »öÀ» ´Ù¸£°Ô ±¸ºÐÇÏ°í ½ÍÀ»¶§ »ç¿ëÇÏ´Â °ÍÀÌ ¹Ù·Î SelectorÀÌ´Ù.

1. Drawable Æú´õ »ý¼º

¾Èµå·ÎÀ̵å ÇÁ·ÎÁ§Æ®¸¦ »ý¼ºÇÏ¸é ±âº»ÀûÀ¸·Î drawable Æú´õ´Â Ãß°¡ µÇ¾î ÀÖÁö ¾Ê±â ¶§¹®¿¡ »ç¿ëÀÚ°¡ Ãß°¡ÇØ ÁÖ¾î¾ßÇÑ´Ù.

resÆú´õ ¹Ø¿¡ drawable µð·ºÅ丮¸¦ »ý¼ºÇÑ´Ù.

2. Color Selector ¸¸µé±â

Color¸¦ ÀÌ¿ëÇÑ Selectorµç Image¸¦ ÀÌ¿ëÇÑ Selector ¸ðµÎ android:drawable="" ¼Ó¼º¿¡ Àû¿ëÇÑ´Ù.

Color Selector´Â »ö»ó°ªÀ» ¹Ù·Î ÀÔ·ÂÇÏ¸é »ç¿ëÀÌ ºÒ°¡´ÉÇÏ°í, color.xmlÀ» ¸¸µé¾î ÁÖ¾î¾ßÇÑ´Ù.

/res/values/ ¹Ø¿¡ color.xml ÆÄÀÏÀ» »õ·Î ¸¸µç´Ù.
 
	<resources>
		<color name="pressed">#B3CA6F</color>
		<color name="nonpressed">#4D5052</color>
	</resources>


/res/drawable/btn_color_selector.xml ÆÄÀÏÀ» ¸¸µç´Ù.
 
	<!--?xml version="1.0" encoding="utf-8"?-->
	<!--color selector-->
	<selector xmlns:android="http://schemas.android.com/apk/res/android">
		<!-- Ŭ¸¯ ÇßÀ» °æ¿ì -->
		<item android:state_pressed="true" android:drawable="@color/pressed"/>

		<!-- Ŭ¸¯ÇÏÁö ¾ÊÀº °æ¿ì -->
		<item android:state_pressed="false" android:drawable="@color/nonpressed"/>
	</selector>


3. Image Selector ¸¸µé±â

pressed(´­·¶À»¶§) À̹ÌÁö¿Í nonpressed(´©¸£Áö ¾Ê¾ÒÀ»¶§) À̹ÌÁö¸¦ res/drawable¿¡ Ãß°¡ÇÑ´Ù.

/res/drawable/btn_image_selector.xml ÆÄÀÏÀ» ¸¸µç´Ù.
 
	<!--?xml version="1.0" encoding="utf-8"?-->
	<!--image selector-->
	<selector xmlns:android="http://schemas.android.com/apk/res/android">
		<!-- Ŭ¸¯ ÇßÀ» °æ¿ì -->
		<item android:state_pressed="true" android:drawable="@drawable/btn_pressed"/>

		<!-- Ŭ¸¯ÇÏÁö ¾ÊÀº °æ¿ì -->
		<item android:state_pressed="false" android:drawable="@drawable/btn_nonpressed"/>
	</selector>


4. Selector Àû¿ëÇϱâ

/res/layout/activity_main.xml ÆÄÀÏÀ» ¸¸µì´Ï´Ù.
 
	<?xml version="1.0" encoding="utf-8"?>
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
				  android:orientation="vertical"
				  android:layout_width="fill_parent"
				  android:layout_height="fill_parent" >
		<Button
				android:layout_weight="2"
				android:text="color"
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:background="@drawable/btn_color_selector"/>
		<Button
				android:layout_weight="2"
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:background="@drawable/btn_image_selector"/>
	</LinearLayout>



Button¿¡ Àû¿ë : android:background=""
ListView¿¡ Àû¿ë : android:listSelector=""


5. Àû¿ë È­¸é





6. Press »óÅ Á¾·ù
android:state_enable : »ç¿ë °¡´É »óÅÂ
android:state_selected : ¼±Åà »óÅÂ
android:state_pressed : Ŭ¸¯ »óÅ (´­·ÁÀÖ´Â °æ¿ì)
android:state_focused : Æ÷Ä¿½º »óÅÂ
android:state_checked : üũµÈ »óÅÂ
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.