2011-11-19 65 views
5

如何以編程方式獲取主題屬性的值?以編程方式獲取主題屬性

例如:

主題:

<style name="Theme"> 
    ... truncated .... 
    <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item> 
</style> 

代碼:

int textSize = ???? // how do I get Theme.textAppearanceLarge? 

編輯:太多的答案是沒有解決的問題。

+0

類似的問題在這裏回答: http://stackoverflow.com/questions/7896615/android-how-to-get-value-of-an-code-in-code – Oderik

+0

[android get textappearance runtime]可能的重複(http://stackoverflow.com/questions/8983629/android-get -textappearance-runtime) – fho

回答

6

使用此功能:

myView.setTextAppearance(Context context, int resid) 
//Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource. 

見:http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance%28android.content.Context,%20int%29

從TextView.java這是上面的功能是什麼:

public void setTextAppearance(Context context, int resid) { 
    TypedArray appearance = 
     context.obtainStyledAttributes(resid, 
             com.android.internal.R.styleable.TextAppearance); 

    int color; 
    ColorStateList colors; 
    int ts; 

    . 
    . 
    . 
    ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable. 
              TextAppearance_textSize, 0); 
    if (ts != 0) { 
     setRawTextSize(ts); 
    } 

    int typefaceIndex, styleIndex; 

    typefaceIndex = appearance.getInt(com.android.internal.R.styleable. 
             TextAppearance_typeface, -1); 
    styleIndex = appearance.getInt(com.android.internal.R.styleable. 
            TextAppearance_textStyle, -1); 

    setTypefaceByIndex(typefaceIndex, styleIndex); 
    appearance.recycle(); 
} 

這是這樣做的另一種方式。確保你回收外觀(TypedArray obect)。希望這可以幫助!

+0

偏離主題,但在這種情況下會有什麼殘留?意思就像R.attrs.bleh。 – user123321

+0

它應該是R.style.textAppearanceLarge根據您的問題。其他你也可以直接使用android.R.style.TextAppearance.Large – rDroid

+0

cool。你有沒有辦法獲得「R.style.textAppearanceLarge」的字面值?我想用它做一些奇特的事情。 – user123321

1

這應該做的伎倆:

int textAppearance = android.R.attr.textAppearanceLarge; 
myTextView.setTextAppearance(context, textAppearance); 
0

我結束了下面的代碼:

public class TextAppearanceConsts 
{ 
    private static final String LOG_TAG = "TextAppearanceConsts"; 

    public static final int[] styleable_TextAppearance; 
    public static final int styleable_TextAppearance_textColor; 
    public static final int styleable_TextAppearance_textSize; 
    public static final int styleable_TextAppearance_typeface; 
    public static final int styleable_TextAppearance_fontFamily; 
    public static final int styleable_TextAppearance_textStyle; 

    static { 
     // F*ing freaking code 
     int ta[] = null, taTc = 0, taTs = 0, taTf = 0, taFf = 0, taTst = 0; 
     try{ 
      Class<?> clazz = Class.forName("com.android.internal.R$styleable", false, TextAppearanceConsts.class.getClassLoader()); 
      ta = (int[])clazz.getField("TextAppearance").get(null); 
      taTc = getField(clazz, "TextAppearance_textColor"); 
      taTs = getField(clazz, "TextAppearance_textSize"); 
      taTf = getField(clazz, "TextAppearance_typeface"); 
      taFf = getField(clazz, "TextAppearance_fontFamily"); 
      taTst = getField(clazz, "TextAppearance_textStyle"); 
     }catch(Exception e){ 
      Log.e(LOG_TAG, "Failed to load styleable_TextAppearance", e); 
     } 
     styleable_TextAppearance = ta; 
     styleable_TextAppearance_textColor = taTc; 
     styleable_TextAppearance_textSize = taTs; 
     styleable_TextAppearance_typeface = taTf; 
     styleable_TextAppearance_fontFamily = taFf; 
     styleable_TextAppearance_textStyle = taTst; 
    } 

    private static int getField(Class<?> clazz, String fieldName) 
    throws IllegalAccessException 
    { 
     try{ 
      return clazz.getField(fieldName).getInt(null); 
     }catch(NoSuchFieldException nsfe){ 
      Log.e(LOG_TAG, nsfe.toString()); 
      return -1; 
     } 
    } 

} 

用例:

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RangeBar, 0, 0); 
TypedArray appearance = null; 
int ap = ta.getResourceId(R.styleable.RangeBar_textAppearance, -1); 
if (ap != -1) { 
    appearance = context.getTheme().obtainStyledAttributes(ap, TextAppearanceConsts.styleable_TextAppearance); 
    int n = appearance.getIndexCount(); 
    for (int i = 0; i < n; i++) { 
     int attr = appearance.getIndex(i); 

     if (attr == TextAppearanceConsts.styleable_TextAppearance_textColor){ 
      mTextColor = appearance.getColorStateList(attr); 
     } else if (attr == TextAppearanceConsts.styleable_TextAppearance_textSize){ 
      mTextSize = appearance.getDimensionPixelSize(attr, mTextSize); 
     } else if (attr == TextAppearanceConsts.styleable_TextAppearance_typeface){ 
      mTypefaceIndex = appearance.getInt(attr, -1); 
     } else if (attr == TextAppearanceConsts.styleable_TextAppearance_fontFamily){ 
      mFontFamily = appearance.getString(attr); 
     } else if (attr == TextAppearanceConsts.styleable_TextAppearance_textStyle){ 
      mFontStyleIndex = appearance.getInt(attr, -1); 
     } else { 
      .... 
     } 
    } 
    appearance.recycle(); 
} 

R.styleable.RangeBar_textAppearance是我的屬性,但您可以訪問Android屬性thi小號方式:

final static String ANDROID_NS = "http://schemas.android.com/apk/res/android"; 
final int textAppearanceResId = attrs.getAttributeResourceValue(ANDROID_NS, "textAppearance", 0); 
.... 
int ap = ta.getResourceId(textAppearanceResId, -1); 

我知道的方法是某種形式的黑客攻擊,但不知道任何其他更好的:(

相關問題