출처:http://blog.naver.com/PostView.nhn?blogId=ezmo01&logNo=110093890027&viewDate=&currentPage=1&listtype=0&userTopListOpen=false&userTopListCount=5&userTopListManageOpen=false&userTopListCurrentPage=undefined
Custom Android Button Style and Theme

In this tutorial, we’ll see how it’s possible to create a custom button style for an Android application by using the Android styling API. First of all we need to define the new look for our custom button style. We would use three different NinePatch drawables.

A custom black nine patch drawable background for enabled, not pressed buttons :

A custom orange nine patch drawable background for enabled, pressed buttons :

A custom red nine patch drawable background for disabled buttons :

All of these three custom drawable must be placed in the /res/drawables directory of our Android application. Now we must declare wich background should be used for each possible state of our custom button (pressed, disabled, focused, …). This is done by declaring a selector in a custom XML file :

2.2기준으로 아래 파일은  res/xml-hdpi/btn_custom.xml에 위치해야함.

<selector xmlns:android="http://schemas.android.com/apk/res/android">  <itemandroid:state_enabled="false" android:drawable="@drawable/btn_red" /> <itemandroid:state_pressed="true" android:state_enabled="true"android:drawable="@drawable/btn_orange" /> <item android:state_focused="true"android:state_enabled="true" android:drawable="@drawable/btn_orange" /> <itemandroid:state_enabled="true" android:drawable="@drawable/btn_black" /> </selector>

Each item in the selector associates a drawable to a button state. Items must be declared in a specific order. The android selector draws the first selector item that match the current button state, no matter if another matching item is defined in the selector declaration. For exemple, the selector bellow will never draw the orange background for a disabled and focused button as the first item always applies to disabled button :

여긴 필요없음

<item android:state_enabled="false" android:drawable="@drawable/btn_red" /> <itemandroid:state_pressed="true" android:state_enabled="false"android:drawable="@drawable/btn_orange" />

In order to draw orange background for disabled and focused buttons, and a red background for disabled buttons that are not focused we should have invert the items declaration order :
여기도 필요없음

<item android:state_pressed="true" android:state_enabled="false"android:drawable="@drawable/btn_orange" /> <item android:state_enabled="false"android:drawable="@drawable/btn_red" />

Let’s put our selector declaration in a file called btn_custom.xml in the /res/drawables directory of our Android application. Now, it’s time to define a custom button style that will apply to every buttons in the application. This can be achieve with both the /res/values/styles.xml file and the /res/values/themes.xml file. In the first one we’ll customize our button appearence (selector, text size, text color, shadow, …).

/res/values/styles.xml 에 위치함 중요한건 아래

name="android:background">@drawable/btn_custom</item> <item 을...

>                                          @xml/btn_custom 으로 바꿔야한다.. 2.2에서 좀 바뀐거 같음.

<resources> <style name="Button" parent="@android:style/Widget.Button"> <itemname="android:gravity">center_vertical|center_horizontal</item> <itemname="android:textColor">#FFFFFFFF</item> <itemname="android:shadowColor">#FF000000</item> <item name="android:shadowDx">0</item><item name="android:shadowDy">-1</item> <itemname="android:shadowRadius">0.2</item> <item name="android:textSize">16dip</item><item name="android:textStyle">bold</item> <itemname="android:background">@drawable/btn_custom</item> <itemname="android:focusable">true</item> <item name="android:clickable">true</item></style> </resources>

Then, we must set this style to be used by the Android button Widget. In the /res/values/themes.xml file :

/res/values/themes.xml   기본파일이 없는지 이해가 안되지만... 만들어주자..

<resources> <style name="CustomButton" parent="android:style/Theme.NoTitleBar"> <itemname="android:buttonStyle">@style/Button</item> </style> </resources>

The theme can be applied to the whole application by setting the android:theme attribute in the<application> tag of the AndroidManifest.xml file.

여긴 써있는데로 메니페스트에서 변경하고...

<application android:icon="@drawable/icon" android:label="Custom button"android:theme="@style/CustomButton">

Now, that’s what your buttons looks like :

You can browse the full source code of the Eclipse project here :
SampleCustomButton

'Android > Tip&Tech' 카테고리의 다른 글

웹뷰 키보드 위치조절(webview)  (0) 2011.11.29
SQL function(함수) 모음  (2) 2011.11.23
Android PopupWindow 관련 팁  (0) 2011.11.23
[펌]Android Traceview War Story  (0) 2011.11.22
android honeycomb 최적화하기  (0) 2011.11.21

+ Recent posts