코드에서 attrs.xml에 생성 된 enum을 얻는 방법
enum 유형의 선언 스타일 속성을 사용 하여 사용자 정의보기 ( 여기에서 찾기 )를 만들었습니다 . xml에서 이제 사용자 지정 속성에 대한 열거 형 항목 중 하나를 선택할 수 있습니다. 이제이 값을 프로그래밍 방식으로 설정하는 메서드를 만들고 싶지만 열거 형에 액세스 할 수 없습니다.
attr.xml
<declare-styleable name="IconView">
<attr name="icon" format="enum">
<enum name="enum_name_one" value="0"/>
....
<enum name="enum_name_n" value="666"/>
</attr>
</declare-styleable>
layout.xml
<com.xyz.views.IconView
android:id="@+id/heart_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="enum_name_x"/>
필요한 것은 다음과 같습니다. mCustomView.setIcon(R.id.enum_name_x);
그러나 열거 형을 찾을 수 없거나 열거 형이나 열거 형의 이름을 어떻게 얻을 수 있는지조차 모릅니다.
속성 enum에서 Java enum을 가져 오는 자동화 된 방법이없는 것 같습니다. Java에서는 지정한 숫자 값을 가져올 수 있습니다. 문자열은 XML 파일에서 사용하기위한 것입니다.
뷰 생성자에서이 작업을 수행 할 수 있습니다.
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.IconView,
0, 0);
// Gets you the 'value' number - 0 or 666 in your example
if (a.hasValue(R.styleable.IconView_icon)) {
int value = a.getInt(R.styleable.IconView_icon, 0));
}
a.recycle();
}
값을 열거 형으로 만들려면 값을 직접 Java 열거 형으로 매핑해야합니다. 예 :
private enum Format {
enum_name_one(0), enum_name_n(666);
int id;
Format(int id) {
this.id = id;
}
static Format fromId(int id) {
for (Format f : values()) {
if (f.id == id) return f;
}
throw new IllegalArgumentException();
}
}
Then in the first code block you could use:
Format format = Format.fromId(a.getInt(R.styleable.IconView_icon, 0)));
(though throwing an exception at this point may not be a great idea, probably better to choose a sensible default value)
It's simple let's show everybody an example just to show how easy it is:
attr.xml:
<declare-styleable name="MyMotionLayout">
<attr name="motionOrientation" format="enum">
<enum name="RIGHT_TO_LEFT" value="0"/>
<enum name="LEFT_TO_RIGHT" value="1"/>
<enum name="TOP_TO_BOTTOM" value="2"/>
<enum name="BOTTOM_TO_TOP" value="3"/>
</attr>
</declare-styleable>
Custom layout:
public enum Direction {RIGHT_TO_LEFT, LEFT_TO_RIGHT, TOP_TO_BOTTOM, BOTTOM_TO_TOP}
Direction direction;
...
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyMotionLayout);
Direction direction = Direction.values()[ta.getInt(R.styleable.MyMotionLayout_motionOrientation,0)];
now use direction like any other enumeration variable.
Well for sanity's sake. Make sure your ordinals are the same in your declared styleable as in your Enum declaration and access it as an array.
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.IconView,
0, 0);
int ordinal = a.getInt(R.styleable.IconView_icon, 0);
if (ordinal >= 0 && ordinal < MyEnum.values().length) {
enumValue = MyEnum.values()[ordinal];
}
I know it's been a while since the question was posted, but I had the same issue recently. I hacked a little something together that uses Square's JavaPoet and some stuff in the build.gradle that automatically creates a Java enum class from the attrs.xml on project build.
There's a little demo and a readme with an explanation at https://github.com/afterecho/create_enum_from_xml
Hope it helps.
'IT story' 카테고리의 다른 글
RabbitMQ 메시지 크기 및 유형 (0) | 2020.09.01 |
---|---|
GitHub와 함께 사용할 프로젝트 관리 (0) | 2020.09.01 |
Windows에 inotify와 같은 것이 있습니까? (0) | 2020.09.01 |
"치명적인 오류 : Objective-C에서 어레이를 연결할 수 없습니다."— 당신은 왜 시도하고 있습니까, Swift? (0) | 2020.09.01 |
바이트 배열이란 무엇을 의미합니까? (0) | 2020.09.01 |