안드로이드에서 버튼을 만드는 방법
- 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/
- 버튼이미지 두 개로 누름효과 적용하기
- res\drawable (없으면 만드다) 에 눌림효과를 줄 button_effect.xml파일을 넣는다.
- button_effect.xml에 소스를 추가
- 눌림과 안눌림 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을 추가해준다.
- 눌림과 안눌림. 포커스 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을 추가해준다.
눌림효과가 적용될 activity_buttontest.xml로 가서
<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" />
위과 같은 부분을 찾는다.
android:background의 값 "@drawable/normal_button"을 "@drawable/button_effect"으로 바꾼다
- 주의할 점은 기존 android:background="@drawable/ ~ "에서 "~" 부분에 이미지 파일이 있었으나 버튼 효과를 줄 때는 xml파일로 바꾸주는 것이다.
'Android 안드로이드' 카테고리의 다른 글
이미지 토글 버튼 (반짝이미지 아님) (0) | 2014.08.08 |
---|---|
안드로이드 소스 버전과 어플 app의 버젼 (0) | 2014.08.03 |
LinearLayout 속성 android:layout_weight (0) | 2014.08.02 |
안드로이드 앱에 AdMob 광고 달기 ][ (0) | 2014.08.02 |
안드로이드 앱에 AdMob 광고 달기 (0) | 2014.08.02 |