2010-06-21 15 views
9

एंड्रॉइड devGuide explains से वर्तमान में लागू थीम में एक विशेषता का मान संदर्भित करें, वर्तमान में लागू विषय में किसी विशेषता के मान को संदर्भित करना संभव है, बजाय प्रश्न-चिह्न (?) का उपयोग करके पर (@)।एंड्रॉइड - वर्तमान में लागू कोड में कोड

क्या कोई यह जानता है कि कोड से ऐसा कैसे करें, उदा। एक अनुकूलित घटक में?

उत्तर

19

एक्सएमएल में, यह कुछ इस तरह दिखेगा:

style="?header_background" 

प्रोग्राम के रूप में, यह एक छोटे जटिल काम है।

private static Theme theme = null; 

protected void onCreate(Bundle savedInstanceState) { 
    ... 
    theme = getTheme(); 
    ... 
} 

public static int getThemeColors(int attr){ 
    TypedValue typedvalueattr = new TypedValue(); 
    theme.resolveAttribute(attr, typedvalueattr, true); 
    return typedvalueattr.resourceId; 
} 

और जब आप विषय की एक विशेषता का उपयोग करना चाहते हैं, तो आप इस तरह कुछ करना होगा: अपनी गतिविधि में

int outside_background = MyActivity.getThemeColors(R.attr.outside_background); 
setBackgroundColor(getResources().getColor(outside_background)); 

यह एक छोटे से अधिक जटिल है, लेकिन वहाँ तुम जाओ ;-)

+1

यह वास्तव में काम नहीं करता है। typedvalueattr.resourceId हमेशा 0 है। क्या आप एक पूर्ण कार्य उदाहरण प्रदान कर सकते हैं? – user123321

+1

मुझे पता था कि वर्तमान में कौन सी थीम लागू की गई थी, यह जानने के बिना एक तरीका होना था। सही काम किया! – DallinDyer

1

उपरोक्त कई कारणों से ऐसा करने का एक अच्छा तरीका नहीं है। NullPointerExceptions एक है।

नीचे ऐसा करने का सही तरीका है।

public final class ThemeUtils { 
    // Prevent instantiation since this is a utility class 
    private ThemeUtils() {} 

    /** 
    * Returns the color value of the style attribute queried. 
    * 
    * <p>The attribute will be queried from the theme returned from {@link Context#getTheme()}.</p> 
    * 
    * @param context the caller's context 
    * @param attribResId the attribute id (i.e. R.attr.some_attribute) 
    * @param defaultValue the value to return if the attribute does not exist 
    * @return the color value for the attribute or defaultValue 
    */ 
    public static int getStyleAttribColorValue(final Context context, final int attribResId, final int defaultValue) { 
     final TypedValue tv = new TypedValue(); 
     final boolean found = context.getTheme().resolveAttribute(attribResId, tv, true); 
     return found ? tv.data : defaultValue; 
    } 
} 

तो बस का उपयोग करने के कार्य करें:

final int attribColor = ThemeUtils.getStyleAttribColorValue(context, R.attr.some_attr, 0x000000 /* default color */); 

बस सुनिश्चित करें कि संदर्भ है कि आप में से पारित गतिविधि से आया हो। GetAplicationContext() नहीं मिलता है या वापस दिया गया थीम एप्लिकेशन ऑब्जेक्ट से होगा, न कि गतिविधि।

0

कई घंटों के बाद मुझे अंततः एक कामकाजी समाधान मिला, ऊपर दिए गए लोगों ने केवल ressourceId लौटाया, रंग नहीं। इसके बजाय आप इस का उपयोग कर सकते हैं:

public static int getThemeColor(Context context, int attr) { 
    TypedValue typedValue = new TypedValue(); 
    context.getTheme().resolveAttribute(attr, typedValue, true); 
    TypedArray ta = context.obtainStyledAttributes(typedValue.resourceId, new int[]{attr}); 
    int color = ta.getColor(0, 0); 
    ta.recycle(); 
    return color; 
} 

क्या आप प्राप्त करना चाहते हैं के साथ बदलें ta.getColor(0, 0), आप उदाहरण के लिए ta.getDimensionPixelSize(0, 0) साथ यह जगह ले सकता है। यदि आप फ़ॉलबैक मान सेट करना चाहते हैं, तो आपको जो भी मूल्य चाहिए, उसके साथ दूसरे 0 को प्रतिस्थापित करें।

संबंधित मुद्दे