【问题标题】:Android: how to get value of an attribute in code?Android:如何在代码中获取属性的值?
【发布时间】:2011-12-15 08:19:04
【问题描述】:

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

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

【问题讨论】:

标签: android


【解决方案1】:

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

【讨论】:

  • 就像一个魅力。只是为什么它必须如此复杂......六年后的现在,是否还有不那么晦涩的方法?
【解决方案2】:

使用

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

【讨论】:

  • typedValue.data 计算结果为:16973890。对于文本大小,这似乎不正确。
  • @ab11 这不是文本大小。它是维度资源的整数。
【解决方案3】:

或者在 kotlin 中:

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。

    【讨论】:

      【解决方案5】:

      这是我的代码。

      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
      

      【讨论】:

        猜你喜欢
        • 2016-02-19
        • 2010-10-24
        • 2016-11-05
        • 2014-01-28
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        相关资源
        最近更新 更多