1. 먼저 attrs.xml에서 사용자 attribute를 정의한다.
2. xml에서 사용자 widget의 속성에 사용자 attribute를 정의한다.
※ 먼저 사용자 widget을 만들어야 합니다.(ex. 간단한 방법은 안드로이드 기본 widget을 상속 함)
3. 소스코드(.java)에서 사용자 attribute 값을 가져온다.
4. 가져온 값을 사용한다.
1. attrs.xml
<resources>
<declare-styleable name="CustomWidget">
<attr name="firstAttribute" format="boolean"/>
<attr name="secondAttribute" format="integer"/>
<attr name="thirdAttribute" format="float"/>
<attr name="fourthAttribute" format="dimension"/>
<attr name="sixthAttribute" format="reference"/>
<attr name="seventhAttribute" format="string"/>
<attr name="eighthAttribute" format="color"/>
<attr name="ninthAttribute" format="fraction"/>
<attr name="tenthAttribute" format="enum"/>
<attr name="eleventhAttribute" format="flag"/>
</declare-styleable>
</resources>
2. main.xml 에서 사용자 widget에 attribute와 사용자 attribute를 추가 합니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom=http://schemas.android.com/apk/res/com.dhna.attributetest
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.dhna.attributetest.CustomWidget
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:firstAttribute="true"
custom:secondAttribute="2"
custom:thirdAttribute="3.5"
custom:forthAttribute="10dp"
custom:sixthAttribute="@drawable/test_drawable"
custom:seventhAttribute="TestAttribute"
custom:eighthAttribute="#FF666666"
ninthAttribute, tenthAttribute, eleventhAttribute 속성은 사용해보지 않았습니다. 필요하신분은 찾아서 해보시길...
3. 소스코드(AttributeTest.java)에서 각 속성에 설정된 값을 가져온다.
가져오는 방법은 첫번째로 순회하는 방법과 두번째로 직접 attribute를 명시해서 가져오는 방법이 있습니다.
[1] 첫번째 방법
public AttributeTest(Context context, AttritubeSet attrs) {
for(int i = 0 ; i < attrs.getAttributeCount() ; i++){
String attributeName = attrs.getAttributeName(i);
if(attributeName == "firstAttribute"){
mBoolean = attrs.getAttributeBooleanValue(i, true);
}else if(attributeName == "secondAttribute"){
mInteger = attrs.getAttributeIntValue(i, 0);
}else if(attributeName == "thirdAttribute"){
mFloat = attrs.getAttributeFloatValue(i, 0.5);
}else if(attributeName == "forthAttribute"){
//mDimension = attrs.getAttributeIntValue(i, 0xFFFFFFFF);
}else if(attributeName == "sixthAttribute"){
mReference= attrs.getAttributeResourceValue(i, R.drawable.icon);
}else if(attributeName == "seventhAttribute"){
mString= attrs.getAttributeValue(i);
}
}
}
...
안되시는건 직접 사용하셔서... 확인 부탁드려요 ㅠ
[2] 두번째 방법
...
public AttributeTest(Context context, AttritubeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomWidget);
mBoolean = typedArray.getBoolean(R.styleable.CustomWidget_firstAttribute, true);
mInteger = typedArray.getInt(R.styleable.CustomWidget_secondAttribute, 0);
mFloat = typedArray.getFloat(R.styleable.CustomWidget_thirdAttribute, 0.5);
mDimension = typedArray.getDimensionPixelSize(R.styleable.CustomWidget_forthAttribute, 0);
mReference = typedArray.getResourceId(R.styleable.CustomWidget_thirdAttribute.sixthAttribute, R.drawable.icon);
mString = typedArray.getString(R.styleable.CustomWidget_seventhAttribute);
typedArray.recycle();
}
...
4. 가져온 값(mBoolean, mInteger, mFloat, ...)을 필요한 곳에 사용하시면 됩니다.

1. 첫번째 방법은 XML에 설정한 모든 속성(Attribute)을 가져 올 수 있습니다.
만약 android의 속성값을 가져오기 위해서는 아래와 같이 하시면 됩니다.
int orientation = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "orientation", 0);
2. 두번째 방법은 사용자가 정의한 속성(Attribute)를 가져오는 방법입니다.
실험해보진 않았지만, 위의 방법으로 해당 속성을 갖은 namespace 부분만
설정한다면 역시 가져 올 수 있을 것이라 생각합니다. 사용자 속성은 두번째
방법이 편할 것이라고 생각되네요.
'안드로이드' 카테고리의 다른 글
View를 상속받은 컨트롤(TextView, ImageView, ...)들 Bitmap으로 변환하기 (3) | 2011.03.30 |
---|---|
안드로이드 속성값인 fill_parent 에서 match_parent 로의 변경 (0) | 2011.03.25 |
이클립스 워크스페이스 설정 (0) | 2011.03.24 |
NotificationBar와 TitleBar의 크기를 알아내는 방법 (0) | 2011.03.20 |
안드로이드 단말기 화면 사이즈 및 해상도 구하기 (2) | 2011.03.17 |
안드로이드 커널 소스에는 있지만 안드로이드 SDK 에서 미제공 되는 함수의 원인 (0) | 2011.03.17 |
Android's StateListDrawable and RadioButton Example (2) | 2011.03.16 |
[지하철정보 만들기] 일정수립 및 개발범위 설정 (2) | 2011.03.13 |
커스텀 속성 - Passing custom attributes via XML resource files (0) | 2011.03.10 |
[Android] Full Build시에 Java API를 추가하거나 수정한 경우 (0) | 2011.03.03 |