Recent Posts
Recent Comments
Link
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

KKGRAM

android studio 버튼 클릭 표현 하기 본문

개발

android studio 버튼 클릭 표현 하기

kkgram 2016. 2. 6. 00:09



android studio 버튼 클릭 표현 하기



따로 뭐 표현 할 말이 딱히 안떠오른다. 그냥 눌렀다는 걸 표시해주기 위해..? a 태그로 보자면 눌렀을 때, 마우스 위로 올렸을 때, 누르고 뗄 때, 평상시 등등 많은데 내가 지금 할 건 버튼을 눌렀을 때의 drawable 변경이다. 이걸 해 주기 위해선 평소 이미지, 눌렀을 때 이미지, 그리고 이들을 selector로 만들어 줄 레이아웃 파일까지 총 세개의 파일이 필요 및 작성을 해야한다. 


나는 둥근 사각형의 버튼을 평상시엔 노란색, 눌렀을 땐 회색으로 변하도록 할 것이다. 이때 drawable는 png의 이미지 파일이 아닌 xml로 작성 한 파일로 할 것이다. 이미지일 경우 drawable 폴더에 필요 이미지를 넣어주면 된다.



round_layout_yellow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/yellowPrimary"/>
    <corners android:radius="10dp"/>
    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>


round_layout_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/grayPrimary"/>
    <corners android:radius="10dp"/>
    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>



혹여 xml을 그대로 붙여 넣을 생각이라면 values>colors.xml 파일에 색 이름에 대해서 정의가 필요하다. 두개의 이미지 혹은 xml이 완성 되었다면 이제 drawable폴더에 xml파일을 하나 생성하여 selector를 작성 해 준다.



selector_btn_primary.xml

<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/round_layout_gray" />
    <item
        android:state_pressed="false"
        android:drawable="@drawable/round_layout_yellow" />
</selector>



이렇게 해 주면 누르지 않은 상태에서는 노란색의 버튼이, 눌렀을 때에는 회색의 버튼이 만들어진다. 이걸 이제 button에 적응시켜보자. 방법은 역시 간단하다.



<Button
    android:id="@+id/btn_ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_btn_primary"
    android:text="OK"/>



이렇게 해 주면 땡. 모두 좀 더 앱스러운 앱을 만들어 보자 ♡⁺◟(●˙▾˙●)◞⁺♡






Comments