【问题标题】:Get theme attributes programmatically以编程方式获取主题属性
【发布时间】:2011-11-19 02:56:14
【问题描述】:

如何以编程方式获取 Theme 属性的值?

例如:

主题:

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

代码:

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

编辑:没有解决问题的答案太多。

【问题讨论】:

标签: android android-theme


【解决方案1】:

使用这个功能:

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)。希望这会有所帮助!

【讨论】:

  • 离题了,但在这种情况下残留物是什么?意思像 R.attrs.bleh。
  • 根据您的问题,它应该是 R.style.textAppearanceLarge 。否则你也可以直接使用 android.R.style.TextAppearance.Large
  • 酷。你有办法获得“R.style.textAppearanceLarge”的字面int值吗?我想用它做一些花哨的事情。
  • 我不知道。但是,在使用 myView.getTextSize() 从主题设置 TextAppearance 之后,您肯定可以获取 textSize
  • 你是如何使用“com.android.internal.R.styleable”的我得到“包com.android.internal.R”不存在。
【解决方案2】:

这应该可以解决问题:

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

【讨论】:

    【解决方案3】:

    我最终得到了以下代码:

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

    我知道这种方法是一种黑客攻击,但不知道其他更好的方法:(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      相关资源
      最近更新 更多