'android:clickable="true"'에 해당되는 글 1건

  1. 2014.08.02 안드로이드 버튼 만드는 속성 clickable과 눌림 효과 적용
Android 안드로이드2014. 8. 2. 22:34

안드로이드에서 버튼을 만드는 방법


  • RalativeLayout, TextView,  ImageView, LinearLayout에 android:clickable="true"를 추가하면 버튼이 된다. Button 만으로 Button 을 만들 필요는 없다.

  • 버튼이미지 하나만으로 누름효과 적용하기
    • http://shiki.me/blog/android-button-background-image-pressedhighlighted-and-disabled-states-without-using-multiple-images/

  • 버튼이미지 두 개로 누름효과 적용하기
    1. res\drawable (없으면 만드다) 에 눌림효과를 줄 button_effect.xml파일을 넣는다.
    2. button_effect.xml에 소스를 추가
      1. 눌림과 안눌림 2개효과 적용할 때
        •  <?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/pressed_button" />

          •  <item android:drawable="@drawable/normal_button" /> 

        • </selector>

        • 효과를 줄 버튼 이미지가 있는 drawable-hdpi폴더 (또는 drawable-xhdpi,  ... )에 버튼 이미지 파일 normal_button, 

        • pressed_button을 추가해준다.

      2. 눌림과 안눌림. 포커스 3개효과 적용할 대
        • <?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/pressed_button" />

          •  <item android:state_focused="true" android:drawable="@drawable/focused_button" />

          •  <item android:drawable="@drawable/normal_button" /> 

        • </selector>

        • 효과를 줄 버튼 이미지가 있는 drawable-hdpi폴더 (또는 drawable-xhdpi,  ... )에 버튼 이미지 normal_button, pressed_button, focused_button을 추가해준다.

      3. 눌림효과가 적용될 activity_buttontest.xml로 가서

      4.     <Button

                android:id="@+id/button1"

                android:layout_width="302dp"

                android:layout_height="105dp"

                android:layout_centerHorizontal="true"

                android:layout_marginTop="144dp"

                android:background="@drawable/normal_button" />

      5. 위과 같은 부분을 찾는다.

      6. android:background의 값 "@drawable/normal_button"을 "@drawable/button_effect"으로 바꾼다

  • 주의할 점은 기존 android:background="@drawable/ ~ "에서 "~" 부분에 이미지 파일이 있었으나 버튼 효과를 줄 때는 xml파일로 바꾸주는 것이다.


Posted by 코드버무려