首页 > 移动平台 > 详细

Android 中的资源访问(Resource)

时间:2015-06-14 02:08:31      阅读:294      评论:0      收藏:0      [点我收藏+]

简单的说,Android中的资源是指非代码部分。例如,在我们的Android程序中要使用一些图片来设置界面,要使用一些音频文件来设置铃声,要使用一些动画来显示特效,要使用一些字符串来显示提示信息。那么,这些图片、音频、动画和字符串等叫做Android中的资源文件。

Eclipse创建的工程中,我们可以看到resassets两个文件夹,是用来保存资源文件的,在assets中保存的一般是原生的文件,例如一个MP3或图片文件,Android程序不能直接访问,必须通过AssetManager类以二进制流的形式来读取。而res中的资源可以通过R资源类直接访问,assets中的资源很少用到,而res中的资源经常使用。

?

资源的类型和布局

目录结构

资源类型

res/anim/

XML动画文件

res/drawable/

一些位图文件

res/layout

XML布局文件

res/values/

各种XML资源文件

arrays.xml:XML数组文件

colors.xml:XML颜色文件

dimenss.xml:XML尺寸文件

styless.xml:XML样式文件

res/xml/

任意的XML文件

res/raw/

直接复制到设备中的原生文件

res/menu/

XML菜单文件

?

R

<!--[if !supportLists]-->l? <!--[endif]-->编译Android应用时,自动生成R类;

<!--[if !supportLists]-->l? <!--[endif]-->该类包含系统中使用的所有资源文件的标识;

<!--[if !supportLists]-->l? <!--[endif]-->资源类:数组array、属性attr、颜色color、图片drawableID标识id、布局layout、字符串string

3.1.1、简介

Android中的资源是在代码中使用的外部文件。这些文件作为应用程序的一部分,被编译到应用程序当中。Android中支持大量的资源文件,如XML文件、图片文件、音频和视频文件。XML文件的格式有不同的写法,详细内容请参考后续小节。

在其他资源中引用资源的一般格式是:

@[包名称:]资源类型 / 资源名称

?

示例:有字符串、颜色、尺寸文件,使用其中的参数

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
  xmlns:android=”http://schemas.android.com/apk/res/android”
   android:orientation=”vertical”
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   android:padding=”@dimen/standard_border”>
  <EditText
   android:id=”@+id/myEditText”
   android:layout_width=”fill_parent”
   android:layout_height=”wrap_content”
   android:text=”@string/stop_message”
   android:textColor=”@color/opaque_blue”
  />
</LinearLayout>

?

3.1.2、颜色(Color)

颜色值定义

<!--[if !supportLists]-->???? <!--[endif]-->通过RGB三原色和一个alpha (透明度)值定义;

<!--[if !supportLists]-->???? <!--[endif]-->#开始,后面是Alpha-Red-Green-Blue格式;

<!--[if !supportLists]-->?? <!--[endif]-->#RGB

<!--[if !supportLists]-->?? <!--[endif]-->#ARGB

<!--[if !supportLists]-->?? <!--[endif]-->#RRGGBB

<!--[if !supportLists]-->?? <!--[endif]-->#AARRGGBB

?

颜色的定义和使用

颜色资源的位置

res/values/colors.xml

颜色XML文件的格式

声明<?xml?version="1.0"?encoding="utf-8"?>

根元素<resourses>?? </resourses>

<color>子元素:<color name=”cname”>value</color>

获取颜色的方法

Resources.getColor()

引用资源的格式

Java代码中:R.color.color_name

XML代码中:@[package:]color/color_name

?

示例:

在该工程的res\values\目录下,定义一个colors.xml颜色资源文件,内容如下所示。

<?xml version="1.0" encoding="UTF-8"?>

<resources>

?????? <color name="color_1">#0000ff</color>

?????? <color name="color_2">#0000dd</color>

</resources>

?

在该工程的res\layout\目录下定义一个布局资源文件,在该文件中添加一个TextView视图组件,引用颜色资源,设置视图组件TextView的文字颜色为蓝色。

3.1.3、字符串(String)

通常情况下,我们可能会使用到大量的字符串作为提示信息,这些字符串都可以作为字符串资源声明在资源配置文件中,从而产现程序的可配置性,避免在代码中进行硬编码。

在代码中我们使用Context.getString()方法,通过传递资源ID参数来得到该字符串,也可以在其他资源文件中引用字符串资源,引用格式为:@string/字符串资源名称。

?

字符串资源XML文件的定义

我们通过表来说明字符串资源是如何定义的,包括资源的位置、XML文件的格式、获得资源的方法和引用资源的方法等。

?

字符串资源得定义和使用

资源位置

res/values/string.xml

字符串XML文件格式

使用<?xml version="1.0" encoding="utf-8"?>

<resources>根元素

<string>子元素:

<string name=color_name>string_value</string>

获得字符串资源的方法

Resources.getString()

引用字符串资源的格式

Java代码中:R.string.string_name

XML文件中:@[package:]string/string_name

?

下面将通过一个实例来演示资源文件的用法。在该实例中用到两个字符串资源:一个在布局文件中引用;另一个在Java代码中引用。实例步骤说明如下。

在该工程的res\values\目录下,创建一个字符串资源文件stirngs.xml,内容如下所示:

<?xml version="1.0" encoding="utf-8"?>

<resources>

?????????? <string name="app_name">Test Resources</string>

?????????? <string name="test_str1">从代码中引用!</string>

?????????? <string name="test_str2">从资源文件引用!</string>

</resources>

?

在该工程的res\layout\目录下,定义一个布局文件test_string.xml。在该布局文件中添加两个TextView视图对象:第一个TextView的文本内容直接引用strings.xml文件中的资源;第二个TextView的文本内容在代码中设置。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

?????????? android:layout_width="fill_parent" android:layout_height="fill_parent">

?????????? <TextView

???????????????????? android:text="@string/test_str1"

???????????????????? android:id="@+id/myTextView01"

???????????????????? android:layout_width="wrap_content"

???????????????????? android:layout_height="wrap_content" />

?????????? <TextView

???????????????????? android:text=""

???????????????????? android:id="@+id/myTextView02"

???????????????????? android:layout_width="wrap_content"

???????????????????? android:layout_height="wrap_content" />

</LinearLayout>

?

创建一个TestStringActivity类。在该类的onCreate()方法中,设置当前的视图布局,并获得TextView实例。通过Context.getString()方法,从字符串资源中获得字符串常量,并将其设置为TextView的文本内容。

public class TestStringActivity extends Activity {

?????????? private TextView myTextView;

?????????? @Override

?????????? public void onCreate(Bundle savedInstanceState) {

???????????????????? super.onCreate(savedInstanceState);

???????????????????? setContentView(R.layout.test_string);

???????????????????? myTextView = (TextView)findViewById(R.id.myTextView02);

???????????????????? String str = getString(R.string.test_str2).toString();

???????????????????? myTextView.setText(str);

?????????? }

}

?

3.1.4、尺寸(Demen)

下面还是通过一个实例来演示尺寸资源的用法。该实例在布局文件中添加一个TextView和一个Button,分别使用尺寸资源文件来定义它们的宽和高。

在工程的res\values\目录下创建一个dimens.xml尺寸资源文件。

<?xml version="1.0" encoding="utf-8"?>

<resources>

??? <dimen name="text_width">150px</dimen>

??? <dimen name="text_height">100px</dimen>

??? <dimen name="btn_width">30mm</dimen>

??? <dimen name="btn_height">10mm</dimen>

</resources>?

?

在工程的res\layout\目录下创建一个test_dimen.xml布局文件。在该布局文件中添加一个TextView和一个ButtonTextView的宽和高引用尺寸资源来设置。Button的宽和高在代码中设置。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical" android:layout_width="fill_parent"

??? android:layout_height="fill_parent">

??? <TextView??

??????? android:text="@string/test_dimen"??

?? ?????android:id="@+id/myDimenTextView01"??

??????? android:layout_width="wrap_content"??

??????? android:layout_height="wrap_content"

??????? android:width="@dimen/text_width"

??????? android:height="@dimen/text_height"

??????? android:background="@color/red_bg"

??????? />?????

??? <Button??

??????? android:text="@string/test_dimen1"??

??????? android:id="@+id/Button01"??

??????? android:layout_width="wrap_content"??

??????? android:layout_height="wrap_content"></Button>???????

</LinearLayout>

?

创建一个TestDimensionActivity类。在该类顶部声明使用的Button视图组件,在onCreate()方法中实例化该组件,并定义尺寸资源设置其宽和高。

public class TestDimensionActivity extends Activity {?

??? private Button myButton;?

??? @Override?

??? public void onCreate(Bundle savedInstanceState) {?

??????? super.onCreate(savedInstanceState);?

?????? // 设置当前Activity的内容布局视图?

?????? setContentView(R.layout.test_dimen);?

?????? // 通过findViewById方法获得Button实例?

?????? myButton = (Button)findViewById(R.id.Button01);?

?????? // 获得Resources 实例?

?????? Resources r = getResources();?

????? // 通过getDimension方法获得尺寸值?

?????? float btn_h = r.getDimension(R.dimen.btn_height);?

?????? float btn_w = r.getDimension(R.dimen.btn_width);?

?????? // 设置按钮的宽?

?????? myButton.setHeight((int)btn_h);?

????? // 设置按钮的高?

?????? myButton.setWidth((int)btn_w);?

??? }?

}

?

3.1.5、原始XML

如果项目中使用到了一些原始的XML文件,那么,我们可以定义一些XML文件供工程使用。XML文件定义在工程的res\xml\目录下,通过Resources.getXML()方法来访问。

?

1、原始XML资源文件定义和使用

资源位置

res/xml/test.xm(文件名称任意)

原始XML文件格式

使用<?xml version="1.0" encoding="utf-8"?>

<resources>根元素

<someElement>子元素:

<someElement name=value/>子元素及属性名称任意

获得XML资源的方法

getResources().getXml()

引用XML资源的格式

Java代码中:R.xml.xml_name

?

2、原始XML文件的使用

?

获得原始XML文件的基本思路是,通过getResources().getXml()获得XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开始还是结尾、是某个标签的开始还是结尾,并通过一些获得属性的方法来遍历XML文件,从而访问XML文件的内容。下面的实例演示了如何访问XML文件内容,并将内容显示在一个TextView中。

?

"Chapter03_Resource"工程中的res\xml\目录下,创建一个test.xml文件。该文件中定义了两条客户信息,属性信息有姓名、年龄、性别和E-mail地址,内容如下所示。

<?xml version="1.0" encoding="utf-8"?>

<resources>

??? <customer name="tom" age="20" gender="male" email="tom@yahoo.com"/>

??? <customer name="kite" age="21" gender="male" email="kite@yahoo.com"/>

</resources>

?

在该工程的res\layout\目录下,创建一个test_xml.xml布局文件,该布局文件中添加一个Button和一个TextView视图组件。Button用于响应单击事件访问XML内容,TextView用于显示XML内容。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical" android:layout_width="fill_parent"

??? android:layout_height="fill_parent">

?????

??? <Button??

??? android:text="获得XML内容"??

??? android:id="@+id/xmltTestButton01"??

??? android:layout_width="wrap_content"??

??? android:layout_height="wrap_content"></Button>

?????

??? <TextView??

??? android:text=""??

??? android:id="@+id/xmlContentTextView01"??

??? android:layout_width="wrap_content"??

??? android:layout_height="wrap_content"

??? />

?????

</LinearLayout>

?

创建一个TestXmlActivity类。在该类顶部声明使用到的TextViewButton,在onCreate()方法中实例化,添加Button的单击事件,获得XML内容显示在TextView中。

import java.io.IOException;?

?

import org.xmlpull.v1.XmlPullParser;?

import org.xmlpull.v1.XmlPullParserException;?

?

import android.app.Activity;?

import android.content.res.Resources;?

import android.content.res.XmlResourceParser;?

import android.os.Bundle;?

import android.view.View;?

import android.view.View.OnClickListener;?

import android.widget.Button;?

import android.widget.TextView;

?

public class TestXmlActivity extends Activity {?

??? private TextView myTextView;?

??? private Button myButton;?

??? @Override?

??? public void onCreate(Bundle savedInstanceState) {?

??????? super.onCreate(savedInstanceState);?

??????? // 设置当前Activity的内容布局视图?

?????? setContentView(R.layout.test_xml);?

??????? // 通过findViewById方法获得TextView实例?

?????? myTextView = (TextView)findViewById(R.id.xmlContentTextView01);?

??????? // 通过findViewById方法获得Button实例?

?????? myButton = (Button)findViewById(R.id.xmltTestButton01);?

??????? // 设置按钮的单击事件监听器?

?????? myButton.setOnClickListener(new OnClickListener() {?

??????? @Override?

??????? public void onClick(View v) {?

?????????????? // 定义计数器?

?????????????? int counter = 0;?

?????????????? // 实例化StringBuilder?

?????????????? StringBuilder sb = new StringBuilder("");?

?????????????? // 获得Resources 实例?

?????????????? Resources r = getResources();?

?????????????? // 通过Resources,获得XmlResourceParser实例?

?????????????????? XmlResourceParser xrp = r.getXml(R.xml.test);?

?????????????? try {?

??????????????????? // 如果没有到文件尾继续循环?

??????????????????? while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {?

??? ????????????????????// 如果是开始标签?

???????????????????????? if (xrp.getEventType() == XmlResourceParser.START_TAG) {?

??????????????????????????? // 获得标签名称??

??????????????????????????? String name = xrp.getName();?

??????????????????????????? // 判断标签名称是否等于customer?

??????????????????????????? if(name.equals("customer")){?

????????????????????????????????? counter++;?

?????????????????????????????????? 获得标签属性追加到StringBuilder?

????????????????????????????????? sb.append(""+counter+"条客户信息:"+"\n");?

? ????????????????????????????????sb.append(xrp.getAttributeValue(0)+"\n");?

????????????????????????????????? sb.append(xrp.getAttributeValue(1)+"\n");?

????????????????????????????????? sb.append(xrp.getAttributeValue(2)+"\n");?

??????????????????????? ??????????sb.append(xrp.getAttributeValue(3)+"\n\n");?

????????????????????????????? }?

???????????????????????? }

??????????????????????????????? ?    else if (xrp.getEventType() == XmlPullParser.END_TAG) {}

???????????????????????????????      else if (xrp.getEventType() == XmlPullParser.TEXT) {}??

????????????????????????? // 下一个标签?

?????????????????????? xrp.next();??

??????????????????? }?

?????????????????? // StringBuilder设置为TextView的文本?

????????????????? myTextView.setText(sb.toString());?

??????????? } catch (XmlPullParserException e) {?

??????????????? e.printStackTrace();?

??????????? } catch (IOException e) {?

??????????????? e.printStackTrace();?

???????? ???}?

??????? }?

??? });?

??? }?

}

?

?

3.1.6、使用drawables资源

drawable资源是一些图片或者颜色资源,主要用来绘制屏幕,通过Resources.get Drawable()方法获得。drawable资源分为三类:Bitmap File(位图文件)、Color Drawable(颜色)、Nine-Patch Image(九片图片)。这里只讲述常用的位图文件的使用。Android中支持的位图文件有pngjpggif

资源位置

res/drawable/file_name.png /file_name.jpg / file_name.gif.

获得位图资源的方法

Resources.getDrawable()

引用位图资源的格式

Java代码中:R.drawable.file_name

XML文件中:@[package:]drawable/file_name

?

下面通过实例的方式来演示位图文件的使用。本实例首先在res\drawable\目录下添加两个位图文件g1.jpgmoto.jpg,并将这两个位图文件显示在ActivityImageView中,第一个通过在布局文件中直接引用,第二个在Java代码中引用。实例步骤说明如下。

一、在工程的res\drawable\目录下添加两张位图文件g1.jpgmoto.jpg

二、创建一个布局文件test_bitmap。在该布局文件中添加两个ImageView组件用来显示图标,其中第一个ImageView组件直接引用g1.jpg文件,第二个在Java代码中进行设置。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:

layout_width="fill_parent" android:layout_height="fill_parent">

?????

??? <TextView??

??????? android:text="测试位图资源"??

??????? android:id="@+id/bitmapTextView01"??

??????? android:layout_width="wrap_content"??

??????? android:layout_height="wrap_content"></TextView>

?

??? <ImageView??

??????? android:id="@+id/bitmapImageView01"??

??????? android:layout_width="wrap_content"??

??????? android:layout_height="wrap_content"

??????? android:src="@drawable/g1"

??????? ></ImageView>

?????

??? <ImageView??

??????? android:id="@+id/bitmapImageView02"??

??????? android:layout_width="wrap_content"??

??????? android:layout_height="wrap_content"></ImageView>

???????? ?

</LinearLayout>

?

三、在工程中创建TestBitmapActivity类。在该类顶部声明一个ImageView视图组件,在onCreate()方法中实例化该组件,并通过Resources.getDrawable()方法获得位图资源,将ImageView组件设置为可显示的图片。

import android.app.Activity;?

import android.content.res.Resources;?

import android.graphics.drawable.Drawable;?

import android.os.Bundle;?

import android.widget.ImageView;?

?

import com.amaker.test.R;?

?

public class TestBitmapActivity extends Activity {?

??? // 声明ImageView对象?

??? private ImageView myImageView;?

??? @Override?

??? public void onCreate(Bundle savedInstanceState) {?

?????? super.onCreate(savedInstanceState);?

?????? // 设置当前内容布局视图?

?????? setContentView(R.layout.test_bitmap);?

?????? // 通过findViewById方法获得ImageView实例?

?????? myImageView = (ImageView)findViewById(R.id.bitm- apImageView02);?

?????? // 获得Resources实例?

?????? Resources r = getResources();?

?????? // 通过Resources 获得Drawable实例?

?????? Drawable d = r.getDrawable(R.drawable.moto);?

?????? // 设置ImageViewImageDrawable属性显示图片?

?????? myImageView.setImageDrawable(d);?

??? }?

}

?

3.1.7、布局(Layout)

布局资源是Android中最常使用的一种资源,Android可以将屏幕中组件的布局方式定义在一个XML文件中,这有点像Web开发中的HTML页面。我们可以调用Activity.setContentView()方法,将布局文件展示在Activity上。Android通过LayoutInflater类将XML文件中的组件解析为可视化的视图组件。布局文件保存在res\layout\文件夹中,文件名称任意。

?

布局文件的定义

资源位置

res/layout/my_layout.xml(文件名称任意)

布局XML文件格式

使用<?xml version="1.0" encoding="utf-8"?>

<布局类

xmlns:android="http://schemas.android.

com/apk/res/android" id="@+id/string_name" (属性)>

<视图组件或者其他嵌套布局类>

<requestFocus/>

</布局类>

获得XML资源的方法

Activity.setContentView()

引用XML资源的格式

Java代码中:R.layout.my_layout

XML文件中:@[package:]layout/my_layout

?

布局文件的使用

下面通过一个实例来演示布局文件的用法。该实例定义一个布局文件,在该布局文件中添加一个TextView、一个EditText和一个Button,分别设置其属性,并且使用Activity.setContentView()方法将其设置为Activity的界面,使用findViewById()方法来得到布局中的组件。

在工程的res\layout\目录下创建一个test_layout.xml布局文件,在该布局文件中使用LinearLayout嵌套TableLayout进行布局管理,其中添加TextViewEditTextButton三个视图组件,并为其设置属性。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout??

??? xmlns:android="http://schemas.android.com/apk/res/android"

??? android:orientation="vertical"??

? ??android:layout_width="fill_parent"

??? android:layout_height="fill_parent">

??? <!-- 以上四个属性分别是命名空间、组件布局方向(这里是垂直)、布局的宽(充满屏幕)和高(充满屏幕)-->

?????

??? <!--? 以下嵌套一个TableLayout -->

??? <TableLayout??

??? android:layout_width="fill_parent"??

??? android:layout_height="fill_parent"??

??? android:stretchColumns="1">

??????? <TableRow>

??????????? <TextView??

??????????? android:text="测试Layout"??

??????????? android:id="@+id/layoutTextView01"??

??????????? android:layout_width="wrap_content"??

?????? ?????android:layout_height="wrap_content"

??????????? android:textColor="@color/red_bg"/>

?????????????

??? <!-- 以上五个属性分别是:文本内容、引用组件的ID、该组件的宽(内容的宽)、该组件的高(内容的高)、文件颜色 -->

?????????

??????????? <EditText??

??????????? android:text=""??

??????????? android:id="@+id/EditText01"??

??????????? android:layout_width="fill_parent"??

??????????? android:layout_height="wrap_content"/>

??????? </TableRow>

?????????

??????? <TableRow android:gravity="right">

??????????? <Button??

??????????? android:text="Test"??

??????????? android:id="@+id/layoutButton01"??

??????????? android:layout_width="wrap_content"??

??????????? android:layout_height="wrap_content"

??????????? />

??????? </TableRow>

??? </TableLayout>

</LinearLayout>

?

在工程中创建一个TestLayoutActivity类,在该类的顶部声明TextViewEditTextButton。在onCreate()方法中定义setContentView()方法,将布局文件设置为Activity的界面,使用findViewById()方法实例化以上三个视图组件。

import android.app.Activity;?

import android.os.Bundle;?

import android.widget.Button;?

import android.widget.EditText;?

import android.widget.TextView;?

import com.amaker.test.R;?

?

public class TestLayoutActivity extends Activity {?

??? private TextView myTextView;?

??? private EditText myEditText;?

??? private Button myButton;?

??? @Override?

??? public void onCreate(Bundle savedInstanceState) {?

??????? super.onCreate(savedInstanceState);?

?????? // 设置Activity的界面布局?

?????? setContentView(R.layout.test_layout);?

?????? // 通过findViewByIdff获得TextView实例?

?????? myTextView = (TextView)findViewById(R.id.layoutText View01);?

?????? // 通过findViewById方法获得EditText实例?

?????? myEditText = (EditText)findViewById(R.id.layoutEdit Text01);?

?????? // 通过findViewById方法获得Button实例?

?????? myButton = (Button)findViewById(R.id.layoutButton01);?

??? }?

}

?

Android 中的资源访问(Resource)

原文:http://finally-m.iteye.com/blog/2219407

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!