- android
- android.accessibilityservice
- android.accounts
- android.content
- android.database.sqlite
- android.graphics
- android.location
- android.media
- android.net
- android.os
- android.text
- android.view
- android.view.inputmethod
- android.widget
- AbsListView
- AbsoluteLayout
- AbsSeekBar
- AbsSpinner
- AdapterView
- AnalogClock
- BaseAdapter
- BaseExpandableListAdapter
- Button
- CheckBox
- CheckedTextView
- Checkable
- Chronometer
- CompoundButton
- CursorAdapter
- CursorTreeAdapter
- DatePicker
- DialerFilter
- DigitalClock
- EditText
- Filter
- Filter.FilterListener
- Filter.FilterResults
- ExpandableListAdapter
- Filterable
- Gallery
- Gallery.LayoutParams
- GridView
- GridLayout
- RadioGroup
- ImageView
- HorizontalScrollView
- ImageButton
- ImageSwitcher
- FilterQueryProvider
- ListAdapter
- ListView
- MediaController
- QuickContactBadge
- RadioButton
- RatingBar
- RelativeLayout
- RemoteViews
- ResourceCursorAdapter
- ResourceCursorTreeAdapter
- Scroller
- ScrollView
- SearchView
- SeekBar
- SeekBar.OnSeekBarChangeListener
- SimpleAdapter
- SimpleCursorAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdapter
- SlidingDrawer
- Spinner
- SpinnerAdapter
- WrapperListAdapter
- TabHost
- TabHost.TabSpec
- TextView
- TimePicker
- Toast
- TableLayout
- TableRow
- TableRow.LayoutParams
- TabWidget
- TextSwitcher
- ToggleButton
- TwoLineListItem
- VideoView
- ViewAnimator
- ViewFlipper
- ViewSwitcher
- ZoomButtonsController
- ZoomButton
- ZoomControls
- dalvik.system
资源访问
资源访问
版本:Android 3.2 r1
原文
http://developer.android.com/guide/topics/resources/accessing-resources.html
资源的调用
当你在应用程序中提供了某种资源后(在 Providing Resources 中有讨论),你就可以通过引用资源 ID 号来调用相关的资源。所有的资源 ID 号都在你工程下的 R.class 文件中定义好,这是由 aapt 工具自动生成的。
当你的应用程序被编译时,aapt 工具就会自动生成 R.class 文件,这个文件中包含有 res/
目录下所有资源的
ID
号。对于每一种资源类型都有一个
R
的子类对应着(例如:
R.drawable
中包含着所有
drawable 资源 ),并且对每个特定类型的所有资源都有一个静态的整型数值一一对应(例如:
R.drawable.icon
)。这个整型数值就是这个特定资源的
ID
号,你可以通过它来获取你的对应资源。
虽然 R.class 文件中定义着每种资源的 ID 号,但是你不应该也不需要到该文件下去查看某个资源的 ID 号。一个资源的 ID 号一般的组成如下:
资源类型: 每种资源都会被分组到一种特定的资源类型,例如 string
, drawable
,和 layout
。要了解更多关于不同的资源类型,见 Resource Types 。
资源名,同时也是:文件名,不包括拓展名;或者是 XML 中 android:name
属性的值,
条件是这个资源是一个简单的值(例如一个字符串)。
调用某个资源有两种方法:
在代码中调用: 通过使用 R.class 的相应子类中的静态整形数值,例如 :
R.string.hello
string
是资源类型,hello 是资源名。当你通过这种方式提供资源的 ID 号时,有很多 Android APIs 就可以调用你的相应资源了。详见: 在代码中调用资源 。
在 XML 中调用: 通过特殊的 XML 语法同样可以对应到你的 R.class 文件中的相关资源 ID,例如:
@string/hello
string
是资源类型,hello 是资源名。你可以在任何需要使用自己提供的资源的地方,通过这种语法在 XML 中调用。详见: 在 XML 中调用资源 。
在代码中调用资源
你可以把资源 ID 号作为方法的参数在代码中调用该资源。例如,你可以设置一个 ImageView 通过调用 setImageResource()
方法来使用
res/drawable/myimage.png
资源:
ImageView imageView = (ImageView) findViewById(R.id.myimageview);imageView.setImageResource( R.drawable.myimage ); |
你也可以通过调用 Resources
类
中的方法来获取某一特定的资源,通过 getResources()
方法可以得到
Resources
类
的一个实例。
语法
这是在代码中引用资源的语法:
[ <package_name> .]R. <resource_type> . <resource_name>
<package_name>
资源所在的包名 (当你要应用自己包下的资源时,该字段不需要填写).
<resource_type>
R 类下对应一种特定资源类型的子类。(译者注:如 R.String)
<resource_name>
可以是不包含文件拓展名的资源文件名或者 XML 元素中 android:name
属性的值(仅限简单的值,如字符串)
了解更多关于每种资源类型的信息以及如何引用它们,见 Resource Types 。
用例
有很多方法可以接受资源的 ID 号作为参数,你可以通过 Resources 中的方法来获取某种对应的资源。你可以通过 Context.getResources() 方法来获得 Resources 类的一个实例。.
以下是在代码中调用资源的一些例子:
// 注:通过一个 drawable 类型的资源给当前屏幕加载背景 getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ; // 注:通过从 Resources 对象获取的字符串给 Activity 设置标题,因为该方法需要一个字符序 列而不是一个资源 ID 号 getWindow().setTitle(getResources().getText(R.string.main_title)); // 注:给当前屏幕载入自定义布局 setContentView(R.layout.main_screen); // 注:通过从 Resources 实例获取的动画来设置一个动画幻灯片 mFlipper.setInAnimation(AnimationUtils.loadAnimation( this , R.anim.hyperspace_in)); // 注:通过资源 ID 号给 TextView 类的对象设置显示文字 TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello_message); |
特别提醒 : 你不能自己手动修改 R.java 文件,这个文件是在你的工程被编译时由 aapt 工具自动生成的。所有程序中的修改都会在工程下一次编译时重写。
在 XML 中调用资源
你可以通过引用一个存在的资源来给一些 XML 的属性和元素指定具体的值。你经常会在创建布局文件的时候这样做,给你的部件提供字符串和图像。
例如,如果你添加一个按钮到你的布局中,你应该使用一个字符串资源来指定显示在按钮上的文字:
<Button android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/submit" /> |
(译者注:@string/submit 即为你定义好的字符串资源)
语法
以下是在 XML 资源文件中引用资源的语法:
@[ <package_name> :] <resource_type> / <resource_name>
<package_name>
资源所在的包名 (当你要应用自己包下的资源时,该字段不需要填写)
<resource_type>
R 类下对应一种特定资源类型的子类.(译者注:如 R.String)
<resource_name>
可以是不包含文件拓展名的资源文件名或者 XML 元素中 android:name
属性的值(仅限简单的值,如字符串)
了解更多关于每种资源类型的信息以及如何引用它们,见 Resource Types 。
用例
在某些情况下你必须得在 XML 中使用资源(例如:给部件应用一个 drawable 图像),但是你可以在 XML 中任何可以接受简单值的地方使用资源。例如,如果你有如下包含 color(颜色)资源和 string(字符串)资源的资源文件:
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <color name= "opaque_red" >#f00</color> <string name= "hello" >Hello!</string> </resources> |
你可以使用这些定义好的资源在以下的布局文件中来设置文本的颜色和内容:
<?xml version= "1.0" encoding= "utf-8" ?> <EditText 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= " @string/hello " /> |
在以下情况下在引用资源时你不需要指定包名,因为这些资源在你自己的当前包中。要引用一个系统资源,你需要在引用时声明资源所在包名。例如:
<?xml version= "1.0" encoding= "utf-8" ?> <EditText xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:textColor= " @android:color/secondary_text_dark " android:text= "@string/hello" /> |
特别提醒: 在需要用字符串时,你应该坚持使用字符串资源,因此你的应用程序就可以对其他语言进行本地化。关于创建可替代资源(例如本地化字符串),见 Providing Alternative Resources 。
你甚至可以可以在 XML 文件中用资源来创建别名。例如,你可以创建一个 drawable 类型的资源,这个资源是另一个 drawable 类型资源的别名:
<?xml version= "1.0" encoding= "utf-8" ?> <bitmap xmlns:android= "http://schemas.android.com/apk/res/android" android:src= "@drawable/other_drawable" /> |
这个听起来像是多余的介绍,但是在使用可替代资源的时候会很有用。了解更多关于 Creating alias resources 。
引用样式属性
一个样式属性资源允许你在当前应用的主题中引用一个属性的值。引用一个样式属性允许你通过样式化 UI 元素以匹配当前主题提供的标准变化来定制 UI 元素的外观,而不是通过提供一个硬编码的值。引用样式属性从基本上说,“使用当前主题中的属性已定义好的样式”。
引用样式属性,其名称的语法与普通的资源格式几乎是等同的,但是取代符号(@),这使用的是问号(?),资源类型部分是可选的。例如:
?[ < package_name > :][ < resource_type > /] < resource_name >
例如,这个例子将介绍如何引用一个属性来设置文本的颜色以匹配系统主题的“主”文本的颜色:
<EditText id= "text" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:textColor= "?android:textColorSecondary" android:text= "@string/hello_world" /> |
在这,android:textColor 属性指定了当前主题中一个样式属性的名称。Android 现在使用应用于 android:textColorSecondary 样式属性的值作为该部件中 android:textColor 的值。因为系统资源工具知道这个属性资源就是环境所期望的,所以你不需要显式地声明是那个类型(这个类型可能是?android:attr/textColorSecondary)—你可以排除掉 attr 类型。
调用平台资源
Android 中包含有很多标准的资源,例如 styles(样式)、themes(主题)、layouts(布局)等等。要调用这些资源,需要通过 android 包名来限定你的资源。例如,Android 提供了一个布局资源,你可以在 ListAdapter 中用以罗列表项。(译者注:ListAdapter 是指列表适配器)
setListAdapter( new ArrayAdapter<String>( this , android.R.layout. simple_list_item_1 , myarray)); |
在这个例子中,simple_list_item_1 是平台为 ListView 的表项定义的布局资源。你可以使用这个列表布局,而不需要自己创建表项布局。(更多关于 ListView,参见List View Tutorial )。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论