본문 바로가기

프로그래밍/Android

TextButton 만들기

1. 커스텀속성을 사용해 추가 스타일 지정

 

res>values 폴더에 my_style.xml 을 하나 생성한다.

버튼에서 사용할 속성을 지정한다. 이러한 커스텀 속성은 아래처럼

추가가 가능하다.

 

이 리소스는 R.styleable 에 생성되는 리소스이며,

R.styleable.StyleName

R.styleable.StyleName_AttrName

형태로 접근할 수 있다.

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

     <declare-styleable name="MyButton">

        <attr name="notFocused"  format="integer"/>

        <attr name="focused"  format="integer"/>

        <attr name="pressed" format="integer"/>

    </declare-styleable>

</resources>

 

위 스타일을 레이아웃에서 사용하려면 새로운 네임스페이스를 하나 추가한다.

워케? xmlns:이름="패키지"

 

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

xmlns:name="http://schemas.android.com/apk/res/my.package.name"

 

해당 뷰에서 name:attr 형태로 사용하면 된다.

<my.package.name.MyButton

name:notFocused="@drawable/red"

name:focused="@drawable/black"

/>

 

2. 투명 이미지를 res>drawable에 추가한다.

empty.png

 

색상과 관련한 리소스를 color 로 추가하던가. 아래처럼 커스텀 리소스로

추가해도 됨.

ex) 커스텀 drawable 리소스

colors.xml

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

 <drawable name="white">#FFFFFF</drawable>

 <drawable name="android_orange">#FF9E18</drawable>

 <drawable name="android_yellow">#F2E406</drawable>

 <drawable name="black">#000000</drawable>

 <drawable name="red">#FF0000</drawable>

</resources>

 

 

 

3. Button 을 상속받은 버튼을 생성한다.

public class MyButton extends ImageButton {

 

 

4. 텍스트 색상을 저장할 변수와 버튼의 눌림상태를 저장할 변수 선언

int notFocusedColor, focusedColor, pressedColor;

boolean isButtonPressed;

 

5. 생성자들을 모두 오버라이드 하고, 초기화를 위해 init 메쏘드를 호출하게 한다.

init 메쏘드는 아래와 같이 1에서 설정한 커스텀 속성에 값이 설정되어 있는지를

확인하게 된다. (레이아웃 xml 에서 커스텀 속성을 설정한 경우는 값이 있것지 ... )

 

private void init( AttributeSet attrs) {

TypedArray a = getContext().obtainStyledAttributes( attrs, R.styleable.MyButton );

String notFocusedStr = a.getString( R.styleable.MyButton_notFocused );

String focusedStr = a.getString( R.styleable.MyButton_focused );

String pressedStr = a.getString( R.styleable.MyButton_pressed );

 

if( notFocusedStr != null && focusedStr != null && pressedStr != null ) {

notFocusedColor = a.getColor( R.styleable.MyButton_notFocused );

focusedColor = a.getColor( R.styleable.MyButton_focused );

pressedColor = a.getColor( R.styleable.MyButton_pressed );

 

} else {

throw new RuntimeException(" ");

}

}

 

6. onTouchEvent 를 오버라이드한다.

@Override

public boolean onTouchEvent( MotionEvent event ) {

if( event.getAction() == MotionEvent.ACTION_DOWN ) {

isButtonPressed = true;

invalidate();

} else if( event.getAction() == MotionEvent.ACTION_UP) {

isButtonPressed = false;

invalidate();

}

 

return super.onTouchEvent(event);

}

 

7. onDraw 를 오버라이드 한다.

@Override

public void onDraw(Canvas canvas) {

if( isButtonPressed ) {

setTextColor( pressedColor );

} else if( isFocused() ) {

setTextColor( focusedColor );

} else {

setTextColor(notFocusedColor);

}

 

super.onDraw(canvas);

}

 

8. 레이아웃 구성

<my.package.name.MyButton

.

.

android:background="@drawable/empty"

 

name:notFocused="@drawable/red"

name:focused="@drawable/black"

.

.

/>

'프로그래밍 > Android' 카테고리의 다른 글

Animation 관련 샘플  (0) 2009.12.06
ViewFlipper로 뷰 교체하기  (0) 2009.12.06
에니메이션 interpolater  (0) 2009.12.06
커스텀속성과 R.styleable  (0) 2009.11.25
Splash window 보이기  (0) 2009.11.24
버튼 상태에 따른 배경변경  (0) 2009.11.22
에니메이션 처리하기  (0) 2009.11.19
투명한 레이아웃 만들기  (0) 2009.11.18
메시지 핸들러 사용하기  (0) 2009.11.16
리시버를 통한 sms데이터 가져오기  (0) 2009.11.16