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


82

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

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

Answers:


127

您的代码仅获得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。


6
奇迹般有效。为何它必须如此复杂……六年之后的现在,还有没有那么晦涩的方法?
Cee McSharpface '17

54

使用

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

1
typedValue.data的计算结果为:16973890。对于文本大小,这似乎不正确。
2011年

@ ab11不是文本大小。它是维度资源的整数。
Sevastyan Savanyuk,

9

或在科特林:

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

4

这似乎是对@ 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 );

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

小心要获取的类型,例如resourceId。


1

这是我的代码。

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.