参考:《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. HBase-存储-HFile格式

    HBase-存储-HFile格式 实际的存储文件功能是由HFile类实现的,它被专门创建以达到一个目的:有效地存储HBase的数据.它们基于Hadoop的TFile类,并模仿Google的BigTab ...

  2. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  3. 爬虫之MongoDB的图片

    聚合:

  4. python global

    如果想在函数内部改变函数外的变量值,用global语句完成: 在不传该变量值入函数的情况下要改变它的值: >>> a = 3 >>> def f(): ... gl ...

  5. 语音01_TTS

    1.http://blog.csdn.net/u010176014/article/details/47428595 2.家里 Win7x64 安装“微软TTS5.1语音引擎(中文).msi”之后,搜 ...

  6. keystone DB in devstack

    ~$ mysql -u -p root mysql> use keystone; mysql> show tables;+------------------------+| Tables ...

  7. (转)Nova中的compute_node

    如需转载,请标明原文出处以及作者 陈锐 RuiChen @kiwik *2015/2/4 22:44:22 * 写在最前面: 这段时间连续改了几个scheduler和resource_tracker相 ...

  8. 判断一个浏览器是否支持opacity

    支持opacity的浏览器,总会将opacity值规范成小于1.0且以0开头的值.例如,如果将opacity指定为:.5,原始支持opacity的浏览器就会将该值规范为0.5,而不支持opacity的 ...

  9. 新东方雅思词汇---7.2、warrant

    新东方雅思词汇---7.2.warrant 一.总结 一句话总结: warr+ant 英 ['wɒr(ə)nt]  美 ['wɔrənt]  n. 根据:证明:正当理由:委任状 vt. 保证:担保:批 ...

  10. EXCEL对比重复数据

    一.       EXCEL 突出重复项 1.      选择对应的数据 EXCEL 里选择好数据 2.      选择条件格式 这样就完成了数据重复的突出,可以按条件筛选.选择自己想要的数据