2012-12-11 6 views
6
내가 어떤 도움을 찾을 수 없습니다 하나 개의 질문을했습니다

:액세스 <선언-styleable> 자원, 프로그램

는 자원 ID를하지 않고, 프로그램의 int []로 보관되고받을 수, 가능 리소스 클래스 R?

int id = context.getResources().getIdentifier("com_facebook_login_view", "declare-styleable", context.getPackageName()); 
int[] resourceIDs = context.getResources().getIntArray(id); 

어떤 생각이 크게 감상 할 수있다 : × 00은 항상 반환 -

<declare-styleable name="com_facebook_login_view"> 
    <attr name="confirm_logout" format="boolean"/> 
    <attr name="fetch_user_info" format="boolean"/> 
    <attr name="login_text" format="string"/> 
    <attr name="logout_text" format="string"/> 
</declare-styleable> 

문제는 내가 정의 '선언 - styleable'속성의 ID를 확인할 수 있다는 것입니다! :)

미리 감사드립니다. 여기에 크리스토퍼

+2

. R.styleable 클래스에 대한 반성을 시도 했습니까? – njzk2

+0

아니요이 말은하지 않았습니다 - 힌트를 주셔서 감사합니다 - 리플렉션을 사용하여 시도해 보겠습니다 :) declare-styleable에 동적으로 액세스 할 수있는 방법이 없습니까? getContext() 메서드를 사용하면 입니다. obtainStyledAttributes (AttributeSet set, int [] attrs); 도움 주셔서 감사합니다. –

+2

해결되었습니다. 그러나 나의 명성은 내 자신의 질문에 대답하기에는 너무 낮습니다. ( 망각에 빠지지 않으면 8 시간 후에 게시 할 것입니다.) –

답변

15

는 아이를 프로그래밍 방식으로 자원-ID를 제공하는 솔루션입니다 - 태그에 정의 된 태그 :

/********************************************************************************* 
* Returns the resource-IDs for all attributes specified in the 
* given <declare-styleable>-resource tag as an int array. 
* 
* @param context  The current application context. 
* @param name  The name of the <declare-styleable>-resource-tag to pick. 
* @return    All resource-IDs of the child-attributes for the given 
*      <declare-styleable>-resource or <code>null</code> if 
*      this tag could not be found or an error occured. 
*********************************************************************************/ 
public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) 
{ 
    try 
    { 
     //use reflection to access the resource class 
     Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields(); 

     //browse all fields 
     for (Field f : fields2) 
     { 
      //pick matching field 
      if (f.getName().equals(name)) 
      { 
       //return as int array 
       int[] ret = (int[])f.get(null); 
       return ret; 
      } 
     } 
    } 
    catch (Throwable t) 
    { 
    } 

    return null; 
} 

어쩌면이 사람 일일 도움이 될 수. :)

인사말

크리스토퍼

+0

그리고 걸림돌에 대한 njzk2에 다시 감사드립니다. :) –

+1

좋은 대답 .. 감사합니다 – Arunkumar

+1

그것은 100 % R.styleable 독립적 인 솔루션을 제공하기 때문에 그것은 받아 들여진 대답이어야합니다. –

1

약간 더 효율적인 솔루션 :

그것이 선언 - styleable 아닌 식별자이기 때문이다
public static final int[] getResourceDeclareStyleableIntArray(String name) { 
     Field[] allFields = R.styleable.class.getFields(); 
     for (Field field : allFields) { 
      if (name.equals(field.getName())) { 
       try { 
        return (int[]) field.get(R.styleable.class); 
       } catch (IllegalAccessException ignore) {} 
      } 
     } 

     return null; 
    }