参考:《Professional
Android 4 Application Development》

Andorid中的资源包括用户自定义资源和系统自带资源,这两种资源既可以在代码中使用,也可以在资源的定义中进行引用。

在代码中使用资源

Android在编译之后会自动生成一个静态类:R。R中包含对应资源类型的静态子类,如R.string, R.drawable。资源则用静态子类的字段表示,字段的名称正是资源的id,例如:R.string.app_name,
R.drawable.icon。示例代码:

// Inflate a layout resource.
setContentView(R.layout.main);
// Display a transient dialog box that displays the error message string resource.
Toast.makeText(this, R.string.app_error, Toast.LENGTH_LONG).show();

Resource类提供了获取各种类型资源的getter方法,例如:

Resources myResources = getResources();
CharSequence styledText = myResources.getText(R.string.stop_message);
Drawable icon = myResources.getDrawable(R.drawable.app_icon);
int opaqueBlue = myResources.getColor(R.color.opaque_blue);
float borderWidth = myResources.getDimension(R.dimen.standard_border);
Animation tranOut;
tranOut = AnimationUtils.loadAnimation(this, R.anim.spin_shrink_fade);
ObjectAnimator animator = (ObjectAnimator)AnimatorInflater.loadAnimator(this, R.anim.my_animator);
String[] stringArray;
stringArray = myResources.getStringArray(R.array.string_array);
int[] intArray = myResources.getIntArray(R.array.integer_array);

在资源内部引用其他资源

使用@可以在资源定义文件内部引用其他资源,其格式如下:

attribute="@[packagename:]resourcetype/resourceidentifier"

示例代码:

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

<LinearLayout

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

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="@dimen/standard_border">

<EditText

android:id="@+id/myEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/stop_message"

android:textColor="@color/opaque_blue"

/>

</LinearLayout>

使用系统资源

Android系统自带了很多资源,我们可以在程序中使用系统的R静态类来获取和使用这些资源。无论在代码中还是在资源内部引用使用,系统资源和用户资源的方式都是一致的,只是引用的R类不同,资源的命名空间不同而已。下面是示例代码:

CharSequence httpError = getString(android.R.string.httpErrorBadUrl);

<EditText

android:id="@+id/myEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@android:string/httpErrorBadUrl"

android:textColor="@android:color/darker_gray"

/>

引用当前主题的资源

Android允许程序

获取当前主题的资源,如颜色等信息,从而使程序员可以方便地提供更为一致的界面,增强用户体验。使用?android:可以获取当前主题下的资源,例如:

<EditText

android:id="@+id/myEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@android:string/httpErrorBadUrl"

android:textColor="?android:textColor"

/>

Android 4学习(4):概述 - Using Resources的更多相关文章

  1. Android UI学习组件概述

    Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...

  2. Android动画学习(二)——Tween Animation

    前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...

  3. Android Animation学习(六) View Animation介绍

    Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...

  4. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  5. Android Animation学习(一) Property Animation原理介绍和API简介

    Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...

  6. Android开发学习之LauncherActivity开发启动的列表

    Android开发学习之LauncherActivity开发启动的列表 创建项目:OtherActivity 项目运行结果:   建立主Activity:OtherActivity.java [jav ...

  7. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  8. Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)

    Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...

  9. Android:日常学习笔记(8)———探究UI开发(5)

    Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...

  10. Android:日常学习笔记(7)———探究UI开发(4)

    Android:日常学习笔记(7)———探究UI开发(4) UI概述  View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...

随机推荐

  1. MapReduce-边数据

    边数据 边数据(side data)是作业所需的额外的只读数据,以辅助处理主数据集.所面临的挑战在于如何使所有map或reduce任务(这些任务散布在集群内部)都能够方便而高效地使用边数据. 利用Jo ...

  2. rpm卸载软件error preun

    这两天,使用ipvsadm -ln总是显示空. 后来,使用strace ipvsadm -ln定位 看来,是ipvsadm模块有问题,卸载了再重新安装吧,结果出现这种问题. 从来没遇到这种问题: er ...

  3. gulp的安装和配置

    gulp的安装和使用方法 1先是有node为前提的, 2安装淘宝镜像 2.1因为很多npm包都是国外的,所以安装起来很慢,所以我们可以利用淘宝的镜像服务器来进行安装后续的包,速度和成功率会高很多.  ...

  4. 通过yum安装mysql

    在linux中安装数据库首选MySQL,Mysql数据库的第一个版本就是发行在Linux系统上,其他选择还可以有postgreSQL,oracle等 在Linux上安装mysql数据库,我们可以去其官 ...

  5. JMeter接口测试报错,反馈和postman不一样(二)

    我总共现在有两个可以学习的接口,昨天测试一个接口发现问题解决后,今天测试另外一个发现又有问题了 这一次还是反馈显示不一样 要么 这种情况是直接从postman里面拿过来的数据,没做处理  报not j ...

  6. review29

    数组流 流的源和目的地除了可以是文件外,还可以是计算机内存. 1.字节数组流 字节数组输入流ByteArrayInputStream和字节数组输出流ByteArrayOutputStream分别使用字 ...

  7. python切片、迭代、生成器、列表生成式等高级特性学习

    python高级特性 1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 切片 当我们要取一个list中的前n各元素时,如果前n个少的话,我们还可以一个一个的取,但是若前n个元 ...

  8. 12-THREE.JS 自然光

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  9. 《Effective C++》——条款20:宁以pass-by-reference-to-const替换pass-by-value

    切割(slicing)问题 请看下面代码: class Window { public: ... std::string name()const; //返回窗口名称 virtual void disp ...

  10. stl_relops.h

    stl_relops.h // Filename: stl_relops.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http ...