Android:如何在代码中获取属性的值?

时间:2011-10-25 22:23:13

标签: android

我想在代码中检索textApperanceLarge的int值。我相信下面的代码是正确的方向,但无法弄清楚如何从TypedValue中提取int值。

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

5 个答案:

答案 0 :(得分:117)

您的代码仅获取 textAppearanceLarge 属性指向的样式的资源ID,即Reno指出的 TextAppearance.Large

要从样式中获取 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();

现在 textSize 将是 textApperanceLarge 指向的样式的文本大小(以像素为单位),如果未设置,则为-1。这假设 typedValue.type 的类型为TYPE_REFERENCE,所以你应该先检查它。

数字 16973890 来自于TextAppearance.Large的资源ID

答案 1 :(得分:46)

使用

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

对于字符串:

typedValue.string
typedValue.coerceToString()

其他数据:

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

在您的情况下,它返回的是TYPE_REFERENCE

我知道应该指向TextAppearance.Large

这是:

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?textColorPrimary</item>
</style>

感谢Martin解决这个问题:

int[] attribute = new int[] { android.R.attr.textSize };
TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
int textSize = array.getDimensionPixelSize(0, -1);

答案 2 :(得分:3)

这似乎是@ user3121370答案的调查问卷。他们烧了。 :o

如果你只需要获得一个维度,比如填充,minHeight(我的情况是:android.R.attr.listPreferredItemPaddingStart)。你可以这样做:

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

就像问题那样,然后:

final DisplayMetrics metrics = new android.util.DisplayMetrics();
WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
int myPaddingStart = typedValue.getDimension( metrics );

就像删除的答案一样。这将允许您跳过处理设备像素大小,因为它使用默认设备指标。返回将是float,你应该转换为int。

想要获得的类型,例如resourceId。

答案 3 :(得分:2)

或者在科特林:

fun Context.dimensionFromAttribute(attribute: Int): Int {
    val attributes = obtainStyledAttributes(intArrayOf(attribute))
    val dimension = attributes.getDimensionPixelSize(0, 0)
    attributes.recycle()
    return dimension
}

答案 4 :(得分:0)

这是我的代码。

public static int getAttributeSize(int themeId,int attrId, int attrNameId)
{
    TypedValue typedValue = new TypedValue();
    Context ctx = new ContextThemeWrapper(getBaseContext(), themeId);

    ctx.getTheme().resolveAttribute(attrId, typedValue, true);

    int[] attributes = new int[] {attrNameId};
    int index = 0;
    TypedArray array = ctx.obtainStyledAttributes(typedValue.data, attributes);
    int res = array.getDimensionPixelSize(index, 0);
    array.recycle();
    return res;
} 

// getAttributeSize(theme, android.R.attr.textAppearanceLarge, android.R.attr.textSize)   ==>  return android:textSize