鲜胎活剥在线观看:android 资源和国际化 - 东东的日志 - 网易博客

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 14:17:26
android 资源和国际化
android相关2009-09-28 15:16:43阅读249评论0  字号:大中小
资源是外部文件(既非代码文件),被代码使用,并且在编译时被编译到你的应用程序中,android 支持的资源,包括XML,PNG,JPNG文件,XML文件会由于其所描述的内容不同而形式不同。
资源从原代码中被抽取出来,基于效率考虑,XML文件被编译成二进制、可以快速加载的形式。字符串,同样被压缩为一种更富效率的存储形式。对于android 程序开发,需要它时知道去那里找这些资源就可以了。
(一)资源
android资源系统了解应用程序中所有非代码资源。使用Resources类访问程序中的资源。一个应用程序的资源在编译时被编译器编译到应用程序中的二进制文件中,要使用一个资源,你必须把安装源文件树中的正确位置,并编译到你应用程序中,作为编译过程的一部分,每个资源的标记都会被生成,在原代码中可要使用这些标记-允许编译器验证应用程序中的代码和定义的资源是否匹配。那如何创建和使用资源呢!
(二)创建资源
android支持字符串、位图以及其他很多中类型的资源。每一种资源的语法、格式以及存放的位置,都会根据其类型的不同而不同。一般来自三种文件:XML(除位图和raw之外的任何文件)、位图文件(图像)以及raw文件(eg:声音.........),事实上,XML文件也有两种不同类型:被原封不动地编译进包内的文件和被aapt 用来产生资源的文件。这里有一个每种资源类型的列表,包括文件格式、文件描述以及XML文件类型的细节。
在项目中的res/目录的适当的子目录中创建和保存资源文件。Android 有一个资源编译器(aapt),它依照资源所在的子目录及其格式对其进行编译。eg:
目录
资源类型
res/anim/
XML文件,它们被编译进frame by frame animation或者tweened animationa对象
res/drawable/
.png、.9.png、.jpg文件,它们被编译进以下的Drawable资源子类型中:
要获得这种类型的一个资源,可以使用Resource.getDrawable(id)
· 9-patches(可变尺寸的位图)
· 位图文件
res/layout/
被编译为屏幕布局(或屏幕的一部分)的XML文件。参见布局(layout)
res/values/
可以被编译成很多种类型的资源的XML文件。
注意:不像其他的res/文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。XML元素类型控制这些资源应该放在R类的什么地方。
尽管这个文件夹里的文件可以任意命名,不过下面是一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中):
 array.xml定义数据
 colors.xml定义color drawable和颜色的字符串值(color string values)。使用Resource.getDrawable()和Resources.getColor()分别获得这些资源。
 dimens.xml定义尺寸值(dimension value)。使用Resources.getDimension()获得这些资源。
 strings.xml定义字符串(string)值(使用Resources.getString()或者Resources.getText()获取这些资源。getText()会保留在UI字符串上应用的丰富的文本样式)。
· styles.xml定义样式(style)对象。
res/xml/
任意的XML文件,在运行时可以通过调用Resources.getXML()读取。
res/raw/
直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。
资源被编译进最终的APK文件中。Android 创建了一个封装类,叫做R,在代码中使用他来引用这些资源。R包含了根据资源文件的路径和名称命名的子类。
全局资源说明(Global Resource Notes)
1、一些资源允许你定义颜色值。android 接受的颜色值可以使用多种web样似的形式-以下集中包含十六进制常数的形式:#RGB,#ARGB,#RRGGBB、#AARRGGBB
2、所有颜色之支持设置透明度(alpha channel value),前两位的十六进制数指定了透明了。0在透明度值是全透明。默认值是不透明。
(三)使用资源
1、代码中使用资源-如何在你代码中对调用资源实例化
R.resource_type.resource_name 或 android.R.resource_type.resource_name
2、从其他资源中引用资源-可以从其他资源中应用资源。重用资源中的公共资源。
@[package:]type/name(@string/hello_world|@android:drawable/opaque_red)
Context.getResource()获得与应用程序相关的Resource实例。图像资源的引用和风格的统一, eg: android:label="@string/app_label" android:icon="@drawable/app_icon"
android:taskAffinity="android.task.calendar">
...............
................
.................

主要是通过android:icon="@drawable/app_icon"中的@drawable/app_icon应用资源,这种技巧还可以创建资源之间的引用。eg:可以创建新的drawable 资源作为已存在资源的别名。


@android:drawable/theme2_background


3、支持针对交替配置的交替资源-根据主机硬件的语言或显示配置指定加载不同的资源。
super.onCreate(icicle);
setContentView(R.layout.main);
这看似很简短的两个java 语句,其实要去深刻的理解他的作用,然后在扩展开来,里面的内容,真的很多,很多,
4、应用主题属性,另外一种资源允许引用当前主题中的属性值。这个属性值只能在样式资源和XML属性中使用;他允许你通过将他们改变为当前主题提供的标准化来改变UI元素的外观,而不是提供具体的值。
引用格式如下:?[namespace:]type/name和“@”,也是可选的!
使用系统资源:android.R.type.id
5、支持对替换(Alternate)语言和配置的替换资源
MyApp/
res/
values-en/
.
.
.
.
.
.
Android 支持几种类型的限定服,每一个都有不同的值。把他们连接在资源文件名称的后面,使用短横线(“-”)隔开。可以为每一个文件夹名称添加多个限定符,但是必须按照这里列出的顺序排列,
eg:
drawable-en-rUS-port-160dpi-finger-qwerty-dpad-480x320/
比较典型的是,你只需指定的要定义资源的配置选项,可以放弃列表中的任何值,但同时要保证生下的值仍然保持列表中的顺序
MyApp/
res/
drawable-en-rUS-finger/
drawable-port/
drawable-port-160dpi
drawable-qwerty
自定义View,eg:API LabelView.java
在res/values/
|_arrays.xml
|_attrs.xml
|_colors.xml
....
关于该目录
(一),可以被编译成很多中类型的资源XML文件
(二),不象其他的/res/folder,他可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身,XML元素类型控制这些资源应该放在R类的什么地方。
(三),尽管这个文件夹里的文件可以任意命名,不过下面这些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中):
arrars.xml 定义数据
colors.xml 定义 color drawable 和颜色的字符串值(color string values).使用Resource.getDrawable()和Resource.getColor()分别获得这些资源。
dimens.xml 定义尺寸值(dimension value),使用Resource.getDimension()获得这些资源值
strings.xml
styles.xml 定义样式(style)对象
如下API中定一的一些文件
arrars.xml





red
orange
yellow
green
blue
violet



Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto



Search Key
Menu Item
Type-To-Search
Disabled



Command one
Command two
Command three
Command four


Map
Satellite
Traffic
Street view


Every Monday
Every Tuesday
Every Wednesday
Every Thursday
Every Friday
Every Saturday
Every Sunday



Alpha Option 01
Beta Option 02
Charlie Option 03



alpha
beta
charlie


attrs.xml













colors.xml



#7f00
#770000ff
#7700ff00
#77ffff00
#ff000000
#e0000000
#00000000
#f00
#0000ff
#f0f0
#ffffff00

LableView.java
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.spt.test.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Example of how to write a custom subclass of View. LabelView
* is used to draw simple text views. Note that it does not handle
* styled text or right-to-left writing systems.
*/
public class LabelView extends View {
private Paint mTextPaint;
private String mText;
private int mAscent;
/**
* Constructor. This version is only needed if you will be instantiating
* the object manually (not from a layout XML file).
* @param context
*/
public LabelView(Context context) {
super(context);
initLabelView();
}
/**
* Construct object, initializing with any attributes we understand from a
* layout file. These attributes are defined in
* SDK/assets/res/any/classes.xml.
*
* @see android.view.View#View(android.content.Context, android.util.AttributeSet)
*/
public LabelView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LabelView);
CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
// Retrieve the color(s) to be used for this view and apply them.
// Note, if you only care about supporting a single color, that you
// can instead call a.getColor() and pass that to setTextColor().
setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
a.recycle();
}
private final void initLabelView() {
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16);
mTextPaint.setColor(0xFF000000);
setPadding(3, 3, 3, 3);
}
/**
* Sets the text to display in this label
* @param text The text to display. This will be drawn as one line.
*/
public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}
/**
* Sets the text size for this label
* @param size Font size
*/
public void setTextSize(int size) {
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
/**
* Sets the text color for this label.
* @param color ARGB value for the text
*/
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
/**
* @see android.view.View#measure(int, int)
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
/**
* Determines the width of this view
* @param measureSpec A measureSpec packed into an int
* @return The width of the view, honoring constraints from measureSpec
*/
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
}
/**
* Determines the height of this view
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
}
/**
* Render the text
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
}
}
custom_view_1.xml



xmlns:app="http://schemas.android.com/apk/res/custom_view_dir"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:background="@drawable/red"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Red"/>
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Blue" app:textSize="20dp"/>
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:text="Green" app:textColor="#ffffffff" />

转自  http://hi.baidu.com/sun_star_mao/blog/item/ca4c86a84377c9f51e17a202.html
android 资源和国际化 - 东东的日志 - 网易博客 人脉资源 - 峡光先锋的日志 - 网易博客 如何嵌入图片资源? - windowsmin的日志 - 网易博客 人民币国际化过程——攘外必先安内 - 靳颉的日志 - 网易博客 上海还不是一个国际化大都市 - 文巽的日志 - 网易博客 不被多数的人知道,但却超好用的东东 - gunailiang的日志 - 网易博客 引用 很好玩的东东 - 优雅女人的日志 - 网易博客 不被多数人知道但却超好的东东 - 伊文君的日志 - 网易博客 多数人不知道但却超好的东东!【生活百科】 - 無為居士的日志 - 网易博客 【引用】多数人不知道但却超好的东东 - 芳草的日志 - 网易博客 android wrapper C调用java api - 示韦BadSmile的日志 - 网易博客 【原创】博客资源素材大全(值得收藏) - 渴望美好的日志 - 网易博客 【原创】博客资源素材大全(值得收藏) - 渴望美好的日志 - 网易博客 【原创】博客资源素材大全(值得收藏) - 渴望美好的日志 - 网易博客 【原创】博客资源素材大全(值得收藏) - 渴望美好的日志 - 网易博客 【原创】博客资源素材大全(值得收藏) - 渴望美好的日志 - 网易博客 实用Android开发工具和资源精选 搜索各大网盘资源的方法 - 圣诞(yy225cn)的日志 - 网易博客 博客代码和素材大全 - ajun2008的日志 - 网易博客. 博客代码和素材大全 - ajun2008的日志 - 网易博客 包头警方破获中国最大一起稀土资源盗窃案 - 真光的日志 - 网易博客 稀有资源小金属上市公司全集[整理] - 角度投资咨询的日志 - 网易博客 pb快速生成资源文件*.pbr - limindo的日志 - 网易博客 【引用】英语节日祝福语(资源来自网络) - 冰雪男生的日志 - 网易博客