参考:《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. tiff/tfw, jpg/jpgw坐标文件的格式(6个参数)

    tiff/tfw, jpg/jpgw坐标文件的格式(6个参数) 0.100-0.13999904400510 以上每行对应的含义: 1 地图单元中的一个象素在X方向上的X分辨率尺度. 2 平移量. 3 ...

  2. 保护SSH的三把锁

    ///////////////////////////////写在前面//////////////////////////////////////原帖地址:http://www.ibm.com/dev ...

  3. postgresql查看数据库占用的物理存储空间大小

    1.手动查看: 查看数据库postgres的oid postgres=# SELECT oid from pg_database where datname='postgres'; oid------ ...

  4. C语言基础三

    C--数组 一维数组的定义和引用 定义:类型说明符 数组名[常量表达式] int a[ 10 ];他表示定义了一个整形数组,数组名为a,有10个元素. 注意:C语言不允许对数组的大小做动态定义. 一维 ...

  5. 完全分布式安装hadoop集群

    0.安装jdk 1.配置hosts文件 2.建立hadoop运行账号 3.配置ssh免密码登录 4.在namenode上配置hadoop 4.1.修改hadoop-env.sh文件 4.2.修改yar ...

  6. sass进阶篇

    @if @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还可以配合 @ ...

  7. brew安装php和扩展

    brew install homebrew/php/php56 --with-apache 报错: checking if the location of ZLIB install directory ...

  8. Codeforces Round #281 (Div. 2) A. Vasya and Football(模拟)

    简单题,却犯了两个错误导致WA了多次. 第一是程序容错性不好,没有考虑到输入数据中可能给实际已经罚下场的人再来牌,这种情况在system测试数据里是有的... 二是chronologically这个词 ...

  9. Hat’s Words(字典树的运用)

    个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...

  10. windowsError:32

    Traceback (most recent call last): File "/usr/lib/python2.7/logging/handlers.py", line 78, ...