2011-11-19 3 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-of-an-attribute-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. 

참조 : TextView.java에서 http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance%28android.content.Context,%20int%29

이 위의 함수가하는 일입니다 :

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 객체)을 재활용해야합니다. 희망이 도움이!

+0

주제를 벗어났습니다. 그러나이 경우에 잔액은 무엇입니까? R.attrs.bleh와 같은 의미입니다. – user123321

+0

질문에 따라 R.style.textAppearanceLarge 여야합니다. 기타 당신도 직접 android.R 스타일을 사용 할수 있습니다 TextAppearance.Large – rDroid

+0

cool. "R.style.textAppearanceLarge"의 리터럴 int 값을 얻는 방법이 있습니까? 나는 그것으로 멋진 것들을하고 싶다. – 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 속성에 액세스 할 수 있습니다. 의 방법 :

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); 

내가 방법은 해킹의 일종 알고 있지만 모르는 다른 더 좋은 일 :(

관련 문제