Android 关键资源的定义和使用
2013-05-29 15:04
381 查看
1. 字符串数组
定义:<resources...> <string-array name = "test_array"> <item>one</item> <item>two</item> <item>three</item> </string-array> </resources>
调用:
Resources res = yourActivity.getResources(); //yourActivity是你的Activity String s[] = res.getStringArray(R.array.test_array);
2.复数资源
定义:<resources...> <plurals name = "eggs_in_a_nest_text"> <item quantity="one">There is 1 egg</item> <item quantity="other">There is %d eggs</item> <item>three</item> </plurals> </resources>
调用:
Resources res = yourActivity.getResources(); //yourActivity是你的Activity String s1 = res.getQuantityString(R.plurals.eggs_in_a_nest_text,0,0);//getQuantityString()的第一个参数是复数ID, //第二个是选择要使用的字符串,为1时,按原样使用该字符串,不为1时,必须提供第三个参数,它的值放在%d的位置 String s1 = res.getQuantityString(R.plurals.eggs_in_a_nest_text,1,1);
3.字符串资源的更多信息
定义字符串资源XML语法<resources> <string name="simple_string">simple string</string> <string name="quoted_string">"quoted 'xyz' string"</string> <string name="double_quoted_string">\"double quotes\"</string> <string name="java_format_string"> hello %2$s Java format string. %1$s again</string> <string name="tagged_string"> Hello <b><i>Slanted Android</i></b>, You are bold. </string> </resources>
在Java代码中使用:
String simpleString = activity.getString(R.string.simple_string); textView.setText(simpleString);
在XML中使用:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/tagged_string"/>
4.颜色资源
在XML中定义:<resources> <color name="red">#f00</color> <color name="blue"#0000ff</color> <color name="green">#f0f0</color> <color name="main_back_ground_color">#ffffff00</color> </resources>
在Java中使用:
int mainBackGroundColor = activity.getResources.getColor(R.color.main_back_ground_color);
在XML中使用:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/red" android:text="Sample Text to Show Red Color"/>
5.尺寸资源
在XML中定义:<resources> <dimen name="mysize_in_pixels">1px</dimen> <dimen name="mysize_in_dp">5dp</dimen> <dimen name="medium_size">100sp</dimen> </resources>
px:像素
in:英寸
mm:毫米
pt:磅
dp:与密度无关的像素,基于160dpi(没英寸的像素数)屏幕(尺寸适应屏幕密度)
sp:与比例无关的像素(这种尺寸支持用户调整大小,适合在字体中使用)
在Java代码中使用:
float dimen = activity.getResources().getDimension(R.dimen.mysize_in_pixels);
在XML中使用:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="@dimen/medium_size"/>
6.图像资源
Android会为在/res/drawable子目录中的图像资源自动生成ID,支持jpg,png,gif。如图像名为sample.jgp,使用时为R.drawable.sample在Java中使用:
BitmapDrawble d = activity.getResources().getDrawable(R.drawable.sample_image); button.setBackgroundDrawable(d); button.setBackgroundResource(R.drawable.sample);
7.色图资源
在Android中,图像是一种图形对象资源。Android支持另一种称为“色图”的图形对象资源,它实际上是一种彩色的矩形。要定义色图资源,是在/res/values/drawable.xml中
<resources> <drawable name="red_rectangle">#f00</drawable> </resources>
在Java中使用:
ColorDrawable redDrawable = (ColorDrawable)activity.getResources().getDrawable(R.drawable.red_rectangle); textView.setBackgroundDrawable(redDrawable);
定义圆角矩形:
在/res/drawable/my_rounded_rectangle.xml中
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0600000"/> <stroke android:width="3dp" color="#ffff8080"/> <corners android:radius="13dp"/> <padding android:left="10dp" android:top="10dp" <android:right="10dp" android:bottom="10dp"/> </shape>
在Java中使用:
GradientDrawable roundedRectangle = (GradientDrawable)activity.getResources().getDrawable(R.drawable.my_rounded_rectangle); textView.setBackgroundDrawable(roundedRectangle);
相关文章推荐
- Android-自己定义图像资源的使用(1)
- Android定义字符串数组资源并在程序中使用
- Android res文件夹下资源定义及使用!
- android如何使用资源文件定义的颜色
- Android res文件夹下资源定义及使用
- android下res目录 资源定义及使用
- Android定义字符串数组资源并在程序中使用
- Android res文件夹下资源定义及使用
- Android 学习笔记4:资源文件的定义和使用
- Android自定义控件使用attr.xml文件中的资源时在布局文件中的定义xmlns:app
- Android res文件夹下资源定义及使用
- Android res文件夹下资源定义及使用
- Android-自己定义图像资源的使用(1)
- Android res文件夹下资源定义及使用
- Android res文件夹下资源定义及使用
- Android res文件夹下资源定义及使用!
- Android中原始资源的使用
- android开发教程之系统资源的使用方法 android资源文件
- Android Studio多渠道打包如何使用不同的资源,依赖和java代码
- Android中使用反射通过资源名字找到资源ID的方法