एंड्रॉइड: कोड में एक विशेषता का मूल्य कैसे प्राप्त करें?


83

मैं कोड में textApperanceLarge का अंतर मान प्राप्त करना चाहूंगा। मेरा मानना ​​है कि नीचे दिया गया कोड सही दिशा में जा रहा है, लेकिन यह पता नहीं लगा सकता है कि टाइपेडवैल्यू से अंतर मूल्य कैसे निकाला जाए।

TypedValue typedValue = new TypedValue(); 
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);

जवाबों:


128

आपका कोड केवल उस शैली की संसाधन आईडी प्राप्त करता है जिसे textAppearanceLarge विशेषता इंगित करती है, अर्थात् TextAppearance.Large जैसा कि रेनो इंगित करता है।

शैली से TextSize विशेषता मान प्राप्त करने के लिए , बस यह कोड जोड़ें:

int[] textSizeAttr = new int[] { android.R.attr.textSize };
int indexOfAttrTextSize = 0;
TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
int textSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
a.recycle();

अब textSize शैली है कि पिक्सल में पाठ का आकार होगा textApperanceLarge के लिए, या -1 अगर यह सेट नहीं था इंगित करता है। यह मान रहा है कि टाइप किया हुआ Value.type टाइप करने के लिए TYPE_REFERENCE था, इसलिए आपको पहले यह देखना चाहिए।

16973890 नंबर इस तथ्य से आता है कि यह TextAppearance.Large की संसाधन आईडी है


6
एक जादू की तरह काम करता है। बस इसे इतना जटिल क्यों होना पड़ता है ... छह साल बाद अब तक कोई कम अस्पष्ट दृष्टिकोण नहीं है?
— Cee McSharpface

55

का उपयोग करते हुए

  TypedValue typedValue = new TypedValue(); 
  ((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);

स्ट्रिंग के लिए:

typedValue.string
typedValue.coerceToString()

अन्य डेटा के लिए:

typedValue.resourceId
typedValue.data  // (int) based on the type

आपके मामले में यह क्या रिटर्न है TYPE_REFERENCE।

मुझे पता है कि यह TextAppearance.Large को इंगित करना चाहिए

जो है :

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?textColorPrimary</item>
</style>

इसे हल करने का श्रेय मार्टिन को जाता है:

int[] attribute = new int[] { android.R.attr.textSize };
TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
int textSize = array.getDimensionPixelSize(0, -1);

1
typedValue.data: 16973890 का मूल्यांकन करता है। यह पाठ आकार के लिए सही नहीं लगता है।
— ab11

@ ab11 यह पाठ का आकार नहीं है। यह आयाम संसाधन का पूर्णांक है।
— सेवस्तन सवण्युक

9

या कोटलिन में:

fun Context.dimensionFromAttribute(attribute: Int): Int {
    val attributes = obtainStyledAttributes(intArrayOf(attribute))
    val dimension = attributes.getDimensionPixelSize(0, 0)
    attributes.recycle()
    return dimension
}

4

यह @ user3121370 के उत्तर पर एक पूछताछ प्रतीत होता है। वे जल गए। : हे

अगर आपको बस एक पैमाना चाहिए, जैसे पैडिंग, माइनहाइट (मेरा मामला था: android.R.attr.listPreferredItemPaddingStart)। तुम कर सकते हो:

TypedValue typedValue = new TypedValue(); 
((Activity)context).getTheme().resolveAttribute(android.R.attr.listPreferredItemPaddingStart, typedValue, true);

जैसे सवाल किया, और फिर:

final DisplayMetrics metrics = new android.util.DisplayMetrics();
WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
int myPaddingStart = typedValue.getDimension( metrics );

बिलकुल हटाए गए उत्तर की तरह। यह आपको डिवाइस पिक्सेल आकार को संभालने की अनुमति देगा, क्योंकि यह डिफ़ॉल्ट डिवाइस मीट्रिक का उपयोग करता है। रिटर्न फ्लोट होगा, और आपको इंट में डालना चाहिए।

आप जिस प्रकार से प्राप्त करने का प्रयास कर रहे हैं, उसके लिए रिसोर्सआईड की तरह बने रहें।


1

यह मेरा कोड है।

public static int getAttributeSize(int themeId,int attrId, int attrNameId)
{
    TypedValue typedValue = new TypedValue();
    Context ctx = new ContextThemeWrapper(getBaseContext(), themeId);

    ctx.getTheme().resolveAttribute(attrId, typedValue, true);

    int[] attributes = new int[] {attrNameId};
    int index = 0;
    TypedArray array = ctx.obtainStyledAttributes(typedValue.data, attributes);
    int res = array.getDimensionPixelSize(index, 0);
    array.recycle();
    return res;
} 

// getAttributeSize(theme, android.R.attr.textAppearanceLarge, android.R.attr.textSize)   ==>  return android:textSize
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.