Android的:如何让在code的属性值? [英] Android: how to get value of an attribute in code?

查看:153
本文介绍了Android的:如何让在code的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找回textApperanceLarge在code的int值。我认为,跌破code是朝着正确的方向发展,但无法弄清楚如何从中提取的TypedValue的int值。

 的TypedValue的TypedValue =新的TypedValue();
((活动)范围内).getTheme()resolveAttribute(android.R.attr.textAppearanceLarge,的TypedValue,真)。
 

解决方案

您code只得到样式的资源ID的 textAppearanceLarge 的属性点,即的 TextAppearance。大的作为雷诺指出。

要获得的 TEXTSIZE 的从风格属性值,只需添加这code:

  INT [] textSizeAttr =新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 的来自于一个事实,这是<一个资源ID href="http://developer.android.com/reference/android/R.style.html#TextAppearance_Large">TextAppearance.Large

I would like to retrieve the int value of textApperanceLarge in code. I believe that the below code is going in the right direction, but can't figure out how to extract the int value from the TypedValue.

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

解决方案

Your code only gets the resource ID of the style that the textAppearanceLarge attribute points to, namely TextAppearance.Large as Reno points out.

To get the textSize attribute value from the style, just add this code:

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

Now textSize will be the text size in pixels of the style that textApperanceLarge points to, or -1 if it wasn't set. This is assuming typedValue.type was of type TYPE_REFERENCE to begin with, so you should check that first.

The number 16973890 comes from the fact that it is the resource ID of TextAppearance.Large

这篇关于Android的:如何让在code的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆