본문 바로가기

JAVA/Android

안드로이드 (이미지)Button 눌렸을때 변화 일으키기 예제 따라하기

- App에서 나만의 이미지의 버튼을 사용하고 싶을때 버튼에 이미지를 입혀 사용하는데, 그때 버튼으로써 눌려졌는지를 확인 할 수 없다. 그렇기 때문에 버튼을 눌렸을때와 누르지 않았을때의 이미지를 다르게 주어서 사용하면 버튼으로써 손색없이 사용 할 수 있게 된다.
- 내 App에 버튼 이미지를 입혀 사용해 보자.

-xml 소스
<!--main.xml-->
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_start" />

</LinearLayout>
-------------------------------------------------------------------------------------------------------
<!--btn_start.xml--> 
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/start2"/>
<item android:state_pressed="false"
android:drawable="@drawable/start"/>
</selector>

- 폴더 구성



- 실행 화면

 <터치 전>

 <터치 하고 있을 때>