2012-12-11 6 views
6

मैं एक सवाल है, जहां मैं किसी भी मदद नहीं मिल सका है:प्रवेश <घोषित-Styleable> संसाधन प्रोग्राम के

इसे प्राप्त करने के संसाधन-आईडी के बिना किसी पूर्णांक [] के रूप में एक से रखा जा रहा प्रोग्राम के संभव है, संसाधन-वर्ग आर को प्रतिबिंबित करना?

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

किसी भी विचार बहुत सराहना की जाएगी: 0x00 हमेशा लौटा दिया जाता है -

<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' विशेषता की आईडी को हल नहीं कर सकता है! :)

अग्रिम धन्यवाद!

क्रिस्टोफर

+2

कि क्योंकि यह एक घोषणा-Styleable, एक पहचानकर्ता नहीं है। क्या आपने आर स्टाइलिएबल क्लास पर प्रतिबिंब का प्रयास किया था? – njzk2

+0

नहीं, मैंने इसका जिक्र नहीं किया - संकेत के लिए धन्यवाद - मैं प्रतिबिंब का उपयोग करके इसे आजमाउंगा :) तो घोषणात्मक-गतिशील गतिशील रूप से पहुंचने का कोई तरीका नहीं है? मैं इसे विधि getContext() प्राप्त करने के लिए उपयोग करूंगा। प्राप्त स्टाइलएट्रिब्यूट्स (विशेषता सेट सेट, int [] attrs); आपकी मदद के लिए धन्यवाद! –

+2

इसे हल किया गया। लेकिन मेरी प्रतिष्ठा मेरे अपने प्रश्न का उत्तर देने के लिए बहुत कम है :( यदि मैं विस्मरण में नहीं आती हूं तो मैं इसे आठ घंटों में पोस्ट करूंगा।) –

उत्तर

15

यहाँ समाधान है कि बच्चे के लिए प्रोग्राम के संसाधन आईडी बचाता है - टैग एक टैग के लिए परिभाषित:

/********************************************************************************* 
* 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

थोड़ा और अधिक कुशल समाधान:

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; 
    } 
संबंधित मुद्दे