欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

常用的getResources()系列

程序员文章站 2022-06-03 21:38:52
...

 

以例子形式说明如何提取资源文件设置view或sting:

private Context ctx;

TextView mText = (TextView)findViewById(R.id.text);
ImageView mImage = (ImageView)findViewById(R.id.image); 
TextView[] mTextViews;
String[] arrays;
String str; 

 

(1)R.dimen.xxx

mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
			ctx.getResources().getDimensionPixelSize(R.dimen.xxx));

 

(2)R.color.xxx

mText.setTextColor(ctx.getResources().getColorStateList(R.color.xxx));
mText.setTextColor(ctx.getResources().getColor(R .color.xxx));

 

(3)R.drawable.xxx

mImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.xxx))

 

(4)R.raw.xxx

InputStream is = ctx.getResources().openRawResource(R.raw.xxx); 

 

(5)R.array.xxx

arrays = getResources().getStringArray(R.array.xxx);

 

(6)R.string.xxx

str = getResources().getString(R.string.xxx)

 

 (7)getIdentifier
用于view的id名称具有相同的部分,如前缀xxx,利用后面的数字进行区别的view

mTextViews = new TextView[LEN]
for (int i = 0; i < mTextViews.length; i++) {
int resId = getResources().getIdentifier("xxx" + i, "id",packageName);
		mTextViews[i] = (TextView) findViewById(resId);}

 

更用Resources应用参看 http://android.toolib.net/reference/android/content/res/Resources.html