출처: http://hansune.tistory.com/551
안드로이드 사용자 정의뷰(Custom View)를 만들고자 할 때 스타일 속성(attr)을 새로 만들어서 사용하게 된다.
그런데 일반적인 속성들(예를 들면 background, maxHeight, max, progress, Etc.)은 안드로이드 정의(android:)를 사용하는게
효율적이고 레이이아웃 xml 작성시에도 편리하다.
예시 고고!
1. android_mimic_attr.xml : 프로그레스바를 예시로 하면 안드로이드 프리픽스(Prefix)로 사용하는 것들을 동일하게 쓴다.
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ProgressBar" > <attr name="android:progressDrawable" /> <attr name="android:minWidth" /> <attr name="android:maxWidth" /> <attr name="android:minHeight" /> <attr name="android:maxHeight" /> <attr name="android:max" /> <attr name="android:progress" /> <attr name="android:secondaryProgress" /> </declare-styleable> <declare-styleable name="SeekBar" > <attr name="android:thumb" /> <attr name="android:thumbOffset" /> <attr name="android:paddingTop" /> <attr name="android:paddingBottom" /> </declare-styleable> <declare-styleable name="Theme" > <attr name="android:disabledAlpha" /> </declare-styleable> </resources> |
2. my_attr.xml : 안드로이드 프리픽스로 해결되지 않는 것들을 정의한다.
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name=“CustomSeekBar" > <attr name="thirdProgress" format="integer" /> <attr name="thirdProgressThick" format="dimension" /> <attr name="thirdProgressDrawable" format="reference" /> </declare-styleable> </resources> |
3. test.xml : 레이아웃 xml 생성.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hansune="http://schemas.android.com/apk/res/com.hansune.sample" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff000000”> <com.hansune.sample.views.CustomSeekBar android:id="@+id/airplayer_seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="2dp" android:layout_marginTop="5dp" android:maxHeight="5dp" android:minHeight="5dp" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" android:max="1000" android:progress="500" android:secondaryProgress="900" hansune:thirdProgressDrawable="@drawable/seekbar_thirdprogress" hansune:thirdProgress="700" hansune:thirdProgressThick="1dp" /> </RelativeLayout> |
4. 이제 커스텀 뷰(CustomSeekBar)에서 어떻게 값을 접근하는지 봅세.
//ProgressBar attr TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyle, 0 ); mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_android_minWidth, mMinWidth); mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_android_maxWidth, mMaxWidth); mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_android_minHeight, mMinHeight); mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_android_maxHeight, mMaxHeight); a.recycle(); //SeekBar attr a = context.obtainStyledAttributes(attrs, R.styleable.SeekBar, defStyle, 0 ); mThumb = a.getDrawable(R.styleable.SeekBar_android_thumb); a.recycle(); //Custom attr a = context.obtainStyledAttributes(attrs, R.styleable.CustomSeekBar, defStyle, 0 ); Drawable drawable = a.getDrawable(R.styleable.CustomSeekBar_thirdProgressDrawable); if (drawable != null ) { setThirdProgressDrawable(drawable); } thirdProgress = a.getInt(R.styleable.CustomSeekBar_thirdProgress, 0 ); thirdProgressThick = a.getDimensionPixelSize(R.styleable.CustomSeekBar_thirdProgressThick , thirdProgressThick); a.recycle |
'IT_Programming > Android_Java' 카테고리의 다른 글
[펌] 안드로이드 JNI 콜백 호출 기법 (0) | 2014.03.31 |
---|---|
[안드로이드] 웹뷰(WebView) 멈춤 현상 (0) | 2014.03.28 |
[펌] AsyncTask와 Activity or Fragment (0) | 2014.03.25 |
[펌] FragmentManager, FragmentTransaction에 대해서! (0) | 2014.03.25 |
[펌] YUV를 Bitmap 으로 바꾸기 (0) | 2014.03.25 |