在代码中如何获取一个属性的值

我想在代码中检索textApperanceLarge的int值。我用的下面的代码,但是并不能从TypedValue提取int值?

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

如何修改代码实现这个方法?

从 style 中获取 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();

你可以参考TextAppearance.Large

使用

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

对于string:

typedValue.string
typedValue.coerceToString()

其它的data:

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

在你的例子中返回的是TYPE_REFERENCE的值。

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