/*---------------------------------------- *-描述--getResources().getXXX()总结. *-描述--S1.getResources() * getResources().getColor(R.color.colorAccent); 获取资源文件颜色资源 * getResources().getString(R.string.app_name); 获取字符串资源 * getResources().getDimension(R.dimen.activity_vertical_margin); 获取布局尺寸资源 * getResources().getDisplayMetrics(); 获取像素相关信息 * getResources().getDrawable(R.drawable.ic_launcher); 获取图片资源 * getResources().getAssets(); 获取未压缩资源 *-描述--S2.; *-描述--None. *-描述--B1.None. * 参考: * http://blog.csdn.net/shulianghan/article/details/19913755 *---------------------------------------------------------------------------*/
/** * 代码中设置布局控件颜色的方法 * 参考: * http://yahaitt.iteye.com/blog/454615 * http://blog.csdn.net/fantianheyey/article/details/9208771 * http://txlong-onz.iteye.com/blog/1249609 * http://my.oschina.net/u/2320057/blog/549507 * 关于各类资源文件 * http://blog.csdn.net/shulianghan/article/details/19913755 * http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0204/838.html */ @SuppressWarnings("ResourceType") @Override public void initDatas() { //1.系统自带的颜色类 tvColorText.setTextColor(Color.GREEN); //2.0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度, //ff00ff表示颜色,注意:这里0xff-ff-00-ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。 tvColorText.setTextColor(0xFFCCCCCC); tvColorText.setTextColor(Color.rgb(55, 55, 55)); tvColorText.setTextColor(Color.parseColor("#87CEFA")); //3.通过获得资源文件进行设置 //可引用资源文件中的颜色属性 //<color name="red">#FF0000</color> //<drawable name="red">#FF0000</drawable> //<string name="red">#FF0000</string> tvColorText.setTextColor(getResources().getColor(R.color.colorAccent)); //数组方式获取资源文件中的颜色 TypedArray typedArray = getResources().obtainTypedArray(R.array.color_array); Drawable drawable = typedArray.getDrawable(0); int color = typedArray.getColor(2, Color.WHITE); tvColorText.setTextColor(color); typedArray.recycle(); }
/** * 获取资源文件中的字符串演示程序2 * @ getResources().getString(R.string.app_name); * @ */ @Override public void initDatas() { LogUtil.infoD(this, "initDatas"); //单个定义方式获取 String string = getResources().getString(R.string.string_test); LogUtil.infoE(this,"string",string); //数组资源方式获取 String[] stringArray = getResources().getStringArray(R.array.string_arr); for(String str : stringArray){ LogUtil.infoE(this,"stringArray",str); } //数组资源方式获取2 String[] stringArray1 = getResources().getStringArray(R.array.string_list); for(String str : stringArray1){ LogUtil.infoE(this,"stringArray1",str); } }
/** * 获取资源文件中的尺寸资源演示程序3 * @ getResources().getDimension(R.dimen.activity_vertical_margin); * @ */ @Override public void initDatas() { LogUtil.infoD(this, "initDatas"); //单点方式获取,获取到的均为像素值 float dimension = getResources().getDimension(R.dimen.dimen_size1); LogUtil.infoE(this,"dimension",dimension+""); //数组方式获取资源文件中的颜色 TypedArray typedArray = getResources().obtainTypedArray(R.array.dimen_arr); float dimen = typedArray.getDimension(0, 0.0f); LogUtil.infoE(this,"dimen",dimen+""); float dimen1 = typedArray.getDimensionPixelSize(0, 0); LogUtil.infoE(this,"dimen1",dimen1+""); typedArray.recycle(); }
/** * 屏幕相关资源信息的获取演示程序4 * @ getResources().getDisplayMetrics(); * @ */ private void getSystemInfo() { //需要上下文 DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); //第一种获取像素密度的方式 int width = metric.widthPixels; // 宽度(PX) int height = metric.heightPixels; // 高度(PX) Log.e("++MyLog++widthPixels ",""+width); Log.e("++MyLog++heightPixels ",""+height); float density = metric.density; // 密度(0.75 / 1.0 / 1.5) int densityDpi = metric.densityDpi; // 密度DPI(120 / 160 / 240) Log.e("++MyLog++density ",""+density); Log.e("++MyLog++densityDpi ",""+densityDpi); //第二种获取像素密度的方式getResources() int height1 = getResources().getDisplayMetrics().heightPixels; // 宽度(PX) int width1 = getResources().getDisplayMetrics().widthPixels; // 高度(PX) Log.e("++MyLog++width1",""+width1); Log.e("++MyLog++height1",""+height1); float density1 = getResources().getDisplayMetrics().density; int densityDpi1 = getResources().getDisplayMetrics().densityDpi; Log.e("++MyLog++density1 ",""+density1); Log.e("++MyLog++densityDpi1 ",""+densityDpi1); float scaledDensity1 = getResources().getDisplayMetrics().scaledDensity; Log.e("++MyLog++scaledDensity1"," "+scaledDensity1); //像素密度X/Ydpi //physical pixels per inch of the screen in the Y dimension float xdpi1 = getResources().getDisplayMetrics().xdpi; float ydpi1 = getResources().getDisplayMetrics().ydpi; Log.e("++MyLog++xdpi1 ",""+xdpi1); Log.e("++MyLog++ydpi1 ",""+ydpi1); }
<resources> <string name="app_name">AndLib</string> <string-array name="string_arr"> <item>字符串1</item> <item>字符串2</item> <item>字符串3</item> <item>字符串4</item> <item>字符串5</item> </string-array> <array name="string_list"> <item>String1</item> <item>String2</item> <item>String3</item> </array> <string name="string_test">字符串</string> <array name="color_array"> <item>@color/colorPrimary</item> <item>@color/colorPrimaryDark</item> <item>@color/colorAccent</item> </array> </resources>