Android 4学习(4):概述 - Using Resources
参考:《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的更多相关文章
- Android UI学习组件概述
Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...
- Android动画学习(二)——Tween Animation
前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...
- Android Animation学习(六) View Animation介绍
Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...
- Android Animation学习(三) ApiDemos解析:XML动画文件的使用
Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...
- Android Animation学习(一) Property Animation原理介绍和API简介
Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...
- Android开发学习之LauncherActivity开发启动的列表
Android开发学习之LauncherActivity开发启动的列表 创建项目:OtherActivity 项目运行结果: 建立主Activity:OtherActivity.java [jav ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
- Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)
Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...
- Android:日常学习笔记(8)———探究UI开发(5)
Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...
- Android:日常学习笔记(7)———探究UI开发(4)
Android:日常学习笔记(7)———探究UI开发(4) UI概述 View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...
随机推荐
- 直播P2P技术1-技术入门
1. 直播协议 直播协议主要有RTMP,HLS,MPEG-DASH,RTSP,HTTP-FLV等.每种协议都各有长短,比如RTMP延迟低,但诞生于Adobe,依赖于Flash Player,在如今FL ...
- 解决Pycharm中matplotlib画图出错问题(AttributeError: module 'matplotlib' has no attribute 'verbose')
最近在Linux中使用pycharm过程中使用matplotlib无法画图,总是提示错误 /usr/bin/python3. /home/leo/PycharmProjects/untitled1/E ...
- pdf2swf+flexpaper解决pdf在线阅读(类百度文库)
1:工具准备swftools.exe 下载 http://www.swftools.org/download.html 安装至D盘 SWFTools提供了一系列将各种文件转成swf的工具: font2 ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
#-*-coding:utf-8-*- import urllib2 from bs4 import BeautifulSoup class dbxs: def __init__(self): sel ...
- hzau 1200 Choosy in Food
1200: Choosy in Food Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 32 Solved: 5[Submit][Status][W ...
- hive从查询中获取数据插入到表或动态分区
Hive的insert语句能够从查询语句中获取数据,并同时将数据Load到目标表中.现在假定有一个已有数据的表staged_employees(雇员信息全量表),所属国家cnty和所属州st是该表的两 ...
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 在Java中定义常量
方法一采用接口(Interface)的中变量默认为static final的特性. 方法二采用了Java 5.0中引入的Enum类型. 方法三采用了在普通类中使用static final修饰变量的方法 ...
- jquery 获取所有父元素
最终结果: 代码: <!DOCTYPE html> <html> <head> <style> b, span, p, html body { padd ...
- UVALive - 3521 Joseph's Problem (整除分块)
给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfl ...