안녕하세요.
R.drawable.a의 경우 아래와 같이 바꾸어서 가지고 올수 있는데요.
context.getResources().getIdentifier("a", "drawable", context.getPackageName());
그런데 R.styleable.XXX 의 경우 int[]로 넘어오는데 아래와 같이 사용을 하려고 하니 리턴값이 0 이 됩니다.
context.getResources().getIntArray(context.getResources().getIdentifier("XXX", "styleable", context.getPackageName()))
R.styleable.XXX 는 attrs.xml에 정의 되어 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="XXX">
... 생략
</declare-styleable>
</resources>
어떻게 해야지 context.getResources().를 사용하여 불러올 수 있을까요?
위와 같이 하려는 이유는 어제 질문을 올린 글(A어플에 B어플 추가)에서 처럼
프로젝트를 jar파일을 만들어서 다른 프로젝트에 추가를 하려고 하니 R.를 쓰면 안된다는 댓글이 있어 변경을 하는 중입니다.
(wini님 감사)
jar파일은 소스파일(class)만 넣고 리소스(res 하위 xml, drawable등)들은 jar파일과 함께 다른 프로젝트에 넘겨야 되는데 위와 같은
문제가 나타났습니다.
조언 부탁드립니다.
give & take
적용이 잘 되지 안네요. ^^;;
R.styleable.XXX 를 아래와 같이 바꾸었습니다.
context.getResources().getIdentifier("XXX", "styleable", context.getPackageName())
그런데 R.styleable.XXX 의 결과는 int[]인데 context.getResources().getIdentifier(~~~)은 int만 반환을 합니다.
그래서 context.getResources().getIntArray(context.getResources() .getIdentifier("XXX", "styleable", context.getPackageName()))
이렇게 해보았더니 문법에서는 오류가 없었는데 실행은 안되네요.
혹시 몰라 로그를 아래와 같이 찍어봤는데
Log.i("TAG", "-- 1 --- : "+ R.styleable.XXX);
Log.i("TAG", "-- 2 --- : "+ context.getResources().getIdentifier("XXX", "styleable", context.getPackageName()));
11-11 13:27:42.740: I/TAG(8511): -- 1 --- : [I@40517048
11-11 13:27:42.740: I/TAG(8511): -- 2 --- : 0
이렇게 찍힙니다.
제가 무언가 잘못을 하고 있는건......
조언 부탁드립니다.
어재도 답변 달아 드렸었는데, 이방법으론 안되나요?
context.getResources().getIdentifier(스타일이름, "styleable ",package) 이런식으로 가져온다음에 getResources().getIntArray(id) 라는 함수가 있네요. 한번 해보시고 결과알려주세요.
안녕하세요 wini님
말씀하셨던 방법으로 해보았는데 오류가 발생을 하였습니다. 위와 동일한 내용으로 오류가 발생하였습니다.
오류 내용 : Caused by: android.content.res.Resources$NotFoundException: Int array resource ID #0x0
계속 방법을 찾고 있는데 많이 힘드네요. ^^;;
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.XXX, defaultStyle, 0);
overlayResource = array.getResourceId(
R.styleable.XXX_[내가 가져오고 싶은 속성], 0);
이거에 대입하셔서 꺼내오셔야 하는데 그냥 꺼내오려고 하신거 같네요
지속적인 답변 감사합니다.
그냥가자님이 말씀하신 부분은 아래와 같이 바꾸어서 사용을 했습니다. (R.을 안쓰기 위해)
TypedArray a =
context.obtainStyledAttributes(attrs
, context.getResources().getIntArray(
context.getResources()
.getIdentifier("XXX", "styleable", context.getPackageName()))
, defStyle, 0);
그리고 다음에서 이렇게 사용을 합니다.
Drawable drawable = a.getDrawable(context.getResources().getIdentifier("XXX_android_xxxDrawable", "styleable", context.getPackageName()));
제가 잘못 쓰고 있는 건가요?
참고 : atts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="XXX">
<attr name="android:xxxDrawable" />
... 생략
</declare-styleable>
</resources>
죄송합니다 제가 실수했네요.... Id가 아니기 때문에
context.getResources().getIntArray(
context.getResources()
.getIdentifier("XXX", "styleable", context.getPackageName())
이렇게 가져오는게 안되는군요...
packageName이 있으니 리플랙션으로 해야 되나....
옛날 질문이지만 저두 같은문제를 해결해야 되서 알려드립니다.
댓글에 있는 리플렉션 기능을 사용하니깐 되네요.
klass = Class.forName(getContext().getPackageName() + ".R");
Class[] inners = klass.getDeclaredClasses();
for(Class f : inners) {
if(f.getSimpleName().equals("styleable")) {
Field[] fields = f.getDeclaredFields();
for(Field field : fields) {
if(field.getName().equals("XXX")) {
try {
return (int[]) field.get(new int[0]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
저건 좀 복잡한데요.... TypedArray를 사용해서 가져오는것으로 알고 있습니다.
원래 용법은 다음과 같습니다.
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.XXX, defaultStyle, 0);
overlayResource = array.getResourceId(
R.styleable.XXX_[내가 가져오고 싶은 속성], 0);
R.styleable.[declare-styleable에 있는 이름] == context.getResources().getIdentifier("XXX", "styleable", context.getPackageName()))
생략된 부분에 YYY가 있다면
R.styleable.XXX_YYY == context.getResources().getIdentifier("XXX_YYY", "styleable", context.getPackageName()))