以编程方式获取主题属性

use*_*321 5 android android-theme

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

例如:

主题:

<style name="Theme">
    ... truncated ....
    <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
</style>
Run Code Online (Sandbox Code Playgroud)

码:

int textSize = ???? // how do I get Theme.textAppearanceLarge? 
Run Code Online (Sandbox Code Playgroud)

编辑:太多的答案没有解决这个问题.

rDr*_*oid 6

使用此功能:

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

请参阅: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();
}
Run Code Online (Sandbox Code Playgroud)

这是另一种方法.确保您回收外观(TypedArray obect).希望这可以帮助!

  • 您如何使用“ com.android.internal.R.styleable”我得到“包com.android.internal.R”不存在。 (2认同)