android中的资源
来源:互联网 发布:肖八肖四选择题 知乎 编辑:程序博客网 时间:2024/05/16 17:09
android的资源是您在代码中使用到的并且在编译时被打包进您的应用程序的附加文件。Android支持多种不同的文件,包括XML、PNG和JPEG文件。XML文件的格式决定于其描述的内容。这些文件将描述文件支持的类型、语法或格式。处于加载效率的考虑,资源被从代码中分离出来,而且XML文件被编译进二进制代码中。字符串,类似的会被压缩存储为更经济的格式。处于这样或那样的原因,Android平台中会存在很多不同的资源和资源定义文件。
Android资源系统保存所有与代码无关资源的存根。您可以使用Resources类访问您应用程序的资源;
与应用程序相关联的资源实例可以通过Context.getResources()得到。
一、创建资源
Android支持字符串,位图和许多其他类型的资源。每一种资源定义文件的语法和格式及保存的位置取决于其依赖的对象。
通常,您可以通过三种文件创建资源:XML文件(除位图和原生文件外),位图文件(作为图片)和原生文件(所有其他的类型,比如声音文件)。
事实上,这里有两种不同类型的XML文件,一种是作为资源被编译进应用程序,另一种是关于资源的描述,被aapt使用。
您可以在您项目res/目录下的适当子目录下创建和存储资源文件。Android使用资源编译器访问资源所在的子目录和格式化的文件。
下面的表格列出了每一种资源的文件类型
res/anim目录
XML文件编译为桢序列动画或者自动动画对象。
res/drawable目录
.png,9.png,.jpg文件被编译为Drawable资源子类型:
使用Resources.getDrawable(id)可以获得资源类型
* 位图文件:png,jpg等普通图片文件
* 9-patchs(可变位图文件):9.png文件
*selector
它主要定义控件在下pressed,selected,focused及平常情况下的属性
更详细参见《selector的使用》
res/layout目录
资源编译为屏幕布局器.
res/values目录
XML 文件可以被编译为多种资源
注意:不像其他res下的目录,这个目录可以包含多个资源描述文件。XML文件元素类型控制着这些资源被R类放置在何处。
这些文件可以自定义名称。这里有一些约定俗成的文件。
* arrays.xml 定义数组。
参见附示例1
* colors.xml 定义可绘制对象的颜色和字符串的颜色。使用Resources.getDrawable()和Resources.getColor()都可以获得这些资源。
参见附示例2
* dimens.xml 定义尺度。使用Resources.getDimension()可以获得这些资源
参见附示例3
* strings.xml 定义字符串(使用Resources.getString()或者更适合的Resources.getText()方法获得这些资源。
Resources.getText()方法将保留所有用于描述用户界面样式的描述符,保持复杂文本的原貌。
参见附示例4
* styles.xml 定义样式对象。
styles就是属性集合。
更详细参见《styles的使用》
*attrs.xml 定义控件对象的属性。
更详细参见《自定义控件属性(attr.xml,TypedArray)》
*themes.xml
详细参见《风格和主题(style,themes)》
res/xml目录
自定义的XML文件。这些文件将在运行时编译近应用程序,并且使用Resources.getXML()方法可以在运行时获取。
res/raw
自定义的原生资源,将被直接拷贝入设备。这些文件将不被压缩近您的应用程序。
使用带有ID参数的Resources.getRawResource()方法可以获得这些资源,比如R.raw.somefilename。
资源被最终编译进APK文件。Android创建包装类R,您可以用他找回资源。R包含一些与资源所在目录同名的子类
Android资源系统保存所有与代码无关资源的存根。您可以使用Resources类访问您应用程序的资源;
与应用程序相关联的资源实例可以通过Context.getResources()得到。
一、创建资源
Android支持字符串,位图和许多其他类型的资源。每一种资源定义文件的语法和格式及保存的位置取决于其依赖的对象。
通常,您可以通过三种文件创建资源:XML文件(除位图和原生文件外),位图文件(作为图片)和原生文件(所有其他的类型,比如声音文件)。
事实上,这里有两种不同类型的XML文件,一种是作为资源被编译进应用程序,另一种是关于资源的描述,被aapt使用。
您可以在您项目res/目录下的适当子目录下创建和存储资源文件。Android使用资源编译器访问资源所在的子目录和格式化的文件。
下面的表格列出了每一种资源的文件类型
res/anim目录
XML文件编译为桢序列动画或者自动动画对象。
res/drawable目录
.png,9.png,.jpg文件被编译为Drawable资源子类型:
使用Resources.getDrawable(id)可以获得资源类型
* 位图文件:png,jpg等普通图片文件
* 9-patchs(可变位图文件):9.png文件
*selector
它主要定义控件在下pressed,selected,focused及平常情况下的属性
更详细参见《selector的使用》
res/layout目录
资源编译为屏幕布局器.
res/values目录
XML 文件可以被编译为多种资源
注意:不像其他res下的目录,这个目录可以包含多个资源描述文件。XML文件元素类型控制着这些资源被R类放置在何处。
这些文件可以自定义名称。这里有一些约定俗成的文件。
* arrays.xml 定义数组。
参见附示例1
* colors.xml 定义可绘制对象的颜色和字符串的颜色。使用Resources.getDrawable()和Resources.getColor()都可以获得这些资源。
参见附示例2
* dimens.xml 定义尺度。使用Resources.getDimension()可以获得这些资源
参见附示例3
* strings.xml 定义字符串(使用Resources.getString()或者更适合的Resources.getText()方法获得这些资源。
Resources.getText()方法将保留所有用于描述用户界面样式的描述符,保持复杂文本的原貌。
参见附示例4
* styles.xml 定义样式对象。
styles就是属性集合。
更详细参见《styles的使用》
*attrs.xml 定义控件对象的属性。
更详细参见《自定义控件属性(attr.xml,TypedArray)》
*themes.xml
详细参见《风格和主题(style,themes)》
res/xml目录
自定义的XML文件。这些文件将在运行时编译近应用程序,并且使用Resources.getXML()方法可以在运行时获取。
res/raw
自定义的原生资源,将被直接拷贝入设备。这些文件将不被压缩近您的应用程序。
使用带有ID参数的Resources.getRawResource()方法可以获得这些资源,比如R.raw.somefilename。
资源被最终编译进APK文件。Android创建包装类R,您可以用他找回资源。R包含一些与资源所在目录同名的子类
很多资源都允许您定义颜色。Android接受web风格的颜色定义-一组十六进制的颜色值,按照以下序列排列:#RGB,#ARGB,#RRGGBB,#AARRGGBB。
*所有的颜色都支持ALPHA通道,头两位十六进制数字指定透明度。0在ALPHA通道中表示全透明,默认值是不透明。
在编译时,Android生成名为R的类。R包含您应用程序所用到的所有资源的索引。这个类包含一些与res下子目录同名的子类。
这些子类包含每一个您在资源文件中定义的资源的标识。这些资源标识可以在您的代码中引用。
注意:R类是自动生成的,并且它不能被手动修改。当资源发生变动时,它会自动修改。
以下是一个生成的R类的示例:
package com.android.samples;
public final class R {
public static final class string {
public static final int greeting=0x0204000e;
public static final int start_button_text=0x02040001;
public static final int submit_button_text=0x02040008;
public static final int main_screen_title=0x0204000a;
};
public static final class layout {
public static final int start_screen=0x02070000;
public static final int new_user_pane=0x02070001;
public static final int select_user_list=0x02070002;
};
public static final class drawable {
public static final int company_logo=0x02020005;
public static final int smiling_cat=0x02020006;
public static final int yellow_fade_background=0x02020007;
public static final int stretch_button_1=0x02020008;
};
};
二、在代码中使用资源
在代码中使用资源需要知道完整的资源ID和您的资源对象类型。下面是资源引用语法:
R.resource_type.resource_name (引用当前应用程序的资源)或者 android.R.resource_type.resource_name(引用android系统的资源)
resource_type是R类中保存制定类型资源的子类。resource_name是定义在XML文件中的资源名或者被其他文件类型所定义的资源文件名(无扩展名)每一类型的资源都依据其类型,被添加入某一指定的R子类;
引用当前应用程序可以不带包名(比如R.resource_type.resource_name)。
Android包含一个标准资源的序号,比如屏幕的样式和按钮的背景。引用这些资源,您必须使用带android的语法,比如android.R.drawable.button_background。
示例1:
// Load a background for the current screen from a drawable resource.
this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
// WRONG Sending a string resource reference into a
// method that expects a string.
this.getWindow().setTitle(R.string.main_title);
// RIGHT Need to get the title from the Resources wrapper.取字符串
this.getWindow().setTitle(Resources.getText(R.string.main_title));
// Load a custom layout for the current screen.
setContentView(R.layout.main_screen);
// Set a slide in animation for a ViewFlipper object.
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_in));
// Set the text on a TextView object.
TextView msgTextView = (TextView)findViewByID(R.id.msg);
msgTextView.setText(R.string.hello_message);
三、引用资源
一个属性值(或资源)同样可以引用资源。这种用法常在资源布局器文件中用于文字和图片(定义在其他文件中)。
这种方法可以引用任何资源,包括颜色和整数。
比如,如果我们有一个颜色资源,我们可以写一个布局器文件,在其中指定文本颜色和尺寸。
示例2:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="Hello, World!" />
注意:“@”前缀声明这是一个资源引用—随后的文本是以@[package:]type/name形式提供的资源名。在这个例子中我们不需要指明特定的包,因为我们在我们自己的包中引用。
四、引用系统资源
引用一个系统资源时,就是@android:type/name的形式。比如示例3.
示例3:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@android:color/opaque_red"
android:text="Hello, World!" />
五、使用系统资源
许多包含于系统之中的资源是能被应用程序所访问的。所有的资源被定义在android.R类中。
比如,您可以使用一下代码在屏幕上显示标准应用程序的ICON
示例5:
public class MyActivity extends Activity
{
public void onStart()
{
requestScreenFeatures(FEATURE_BADGE_IMAGE);
super.onStart();
setBadgeResource(android.R.drawable.sym_def_app_icon);
}
}
类似的,下面的代码会更改您的系统主题
示例6:
public class MyActivity extends Activity
{
public void onStart()
{
super.onStart();
setTheme(android.R.style.Theme_Black);
}
}
*所有的颜色都支持ALPHA通道,头两位十六进制数字指定透明度。0在ALPHA通道中表示全透明,默认值是不透明。
在编译时,Android生成名为R的类。R包含您应用程序所用到的所有资源的索引。这个类包含一些与res下子目录同名的子类。
这些子类包含每一个您在资源文件中定义的资源的标识。这些资源标识可以在您的代码中引用。
注意:R类是自动生成的,并且它不能被手动修改。当资源发生变动时,它会自动修改。
以下是一个生成的R类的示例:
package com.android.samples;
public final class R {
public static final class string {
public static final int greeting=0x0204000e;
public static final int start_button_text=0x02040001;
public static final int submit_button_text=0x02040008;
public static final int main_screen_title=0x0204000a;
};
public static final class layout {
public static final int start_screen=0x02070000;
public static final int new_user_pane=0x02070001;
public static final int select_user_list=0x02070002;
};
public static final class drawable {
public static final int company_logo=0x02020005;
public static final int smiling_cat=0x02020006;
public static final int yellow_fade_background=0x02020007;
public static final int stretch_button_1=0x02020008;
};
};
二、在代码中使用资源
在代码中使用资源需要知道完整的资源ID和您的资源对象类型。下面是资源引用语法:
R.resource_type.resource_name (引用当前应用程序的资源)或者 android.R.resource_type.resource_name(引用android系统的资源)
resource_type是R类中保存制定类型资源的子类。resource_name是定义在XML文件中的资源名或者被其他文件类型所定义的资源文件名(无扩展名)每一类型的资源都依据其类型,被添加入某一指定的R子类;
引用当前应用程序可以不带包名(比如R.resource_type.resource_name)。
Android包含一个标准资源的序号,比如屏幕的样式和按钮的背景。引用这些资源,您必须使用带android的语法,比如android.R.drawable.button_background。
示例1:
// Load a background for the current screen from a drawable resource.
this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
// WRONG Sending a string resource reference into a
// method that expects a string.
this.getWindow().setTitle(R.string.main_title);
// RIGHT Need to get the title from the Resources wrapper.取字符串
this.getWindow().setTitle(Resources.getText(R.string.main_title));
// Load a custom layout for the current screen.
setContentView(R.layout.main_screen);
// Set a slide in animation for a ViewFlipper object.
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_in));
// Set the text on a TextView object.
TextView msgTextView = (TextView)findViewByID(R.id.msg);
msgTextView.setText(R.string.hello_message);
三、引用资源
一个属性值(或资源)同样可以引用资源。这种用法常在资源布局器文件中用于文字和图片(定义在其他文件中)。
这种方法可以引用任何资源,包括颜色和整数。
比如,如果我们有一个颜色资源,我们可以写一个布局器文件,在其中指定文本颜色和尺寸。
示例2:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="Hello, World!" />
注意:“@”前缀声明这是一个资源引用—随后的文本是以@[package:]type/name形式提供的资源名。在这个例子中我们不需要指明特定的包,因为我们在我们自己的包中引用。
四、引用系统资源
引用一个系统资源时,就是@android:type/name的形式。比如示例3.
示例3:
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="@android:color/opaque_red"
android:text="Hello, World!" />
五、使用系统资源
许多包含于系统之中的资源是能被应用程序所访问的。所有的资源被定义在android.R类中。
比如,您可以使用一下代码在屏幕上显示标准应用程序的ICON
示例5:
public class MyActivity extends Activity
{
public void onStart()
{
requestScreenFeatures(FEATURE_BADGE_IMAGE);
super.onStart();
setBadgeResource(android.R.drawable.sym_def_app_icon);
}
}
类似的,下面的代码会更改您的系统主题
示例6:
public class MyActivity extends Activity
{
public void onStart()
{
super.onStart();
setTheme(android.R.style.Theme_Black);
}
}
六、附件
附示例1:
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="twarr_indexlist">
<!-- <item>!@#</item> -->
<!-- <item>123</item> -->
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>
<string-array name="arr_indexlist_clock">
<item>+13</item>
<item>+12</item>
<item>+11</item>
</string-array>
</resources>
附示例2:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_color">#00000000</color>
</resources>
附示例3:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="test_textsize_default">28sp</dimen>
<dimen name="test_itemheight_default">63dp</dimen>
</resources>
附示例4:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,\n Hello!</string>
<string name="hi">Hi,hubin!</string>
</resources>
附示例1:
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="twarr_indexlist">
<!-- <item>!@#</item> -->
<!-- <item>123</item> -->
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>
<string-array name="arr_indexlist_clock">
<item>+13</item>
<item>+12</item>
<item>+11</item>
</string-array>
</resources>
附示例2:
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_color">#00000000</color>
</resources>
附示例3:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="test_textsize_default">28sp</dimen>
<dimen name="test_itemheight_default">63dp</dimen>
</resources>
附示例4:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,\n Hello!</string>
<string name="hi">Hi,hubin!</string>
</resources>
- Android中的资源分析
- Android中的资源分析
- android中的资源
- Android 中的资源访问
- Android中的资源访问
- android系统中的资源
- Android中的Drawable资源
- Android中的资源文件
- android 中的 res 资源
- Android中的资源文件
- Android中的Drawable资源
- Android中的资源布局
- Android中的资源访问
- Android中的Drawable资源
- Android中的图片资源
- Android 中的Drawable资源
- Android中的Drawable资源
- Android中的动画资源
- java 集合ArrayList及LinkList源码分析
- C#中DefaultValueAttribute的使用
- uCos中优先级判定表OSUnMapTbl原理
- 如何实现iframe高度的自适应
- 2011面试题目之猴子偷桃
- android中的资源
- Android Path 使用
- 人人都能做的性能优化 web前端优化
- 线程1
- setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
- 要生存,先做一株小草
- QT中文乱码问题解决办法
- Android2.3.5系统源码下载
- 在线制作图片