好久没更新博客了,今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示:

我们主要看一下layout与values目录,layout里的xml文件的我们应用使用布局的文件,values里的sring.xml是用来存放文字资源,一个key对应一个value值。

但是在实际应用开发中,通常横屏(land)与竖屏(port)可能布局文件有所不同,这时候我们可以独自定义横屏与竖屏的布局文件( 文件名字要一样),默认情况是加载layout目录里的布局文件。同样应用还要支持不同的语言,如果我们应用里没有定义手机所用语言的资源时,会默认加载values的值。

为了方便大家理解下面做个简单的Demo.具体步骤如下:

第一步:新建一个Android工程,命名为ResourceDemo。

我们看一下layout目录下的自动生成的main.xml布局文件,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. </LinearLayout>

其中我们程序显示的文本内容是在values/string.xml文件中对应的hello的值,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,ResourceDemo!</string>
  4. <stringname="app_name">ResourceDemo</string>
  5. </resources>

运行效果如下图所示:

port(竖屏模式)效果:

Ctrl+F11快捷键模拟器变成横屏(land)模式:

第二步:我们定义land与port模式的布局文件,即在res/目录下创建layout-land与layout-port两个文件夹,目录结果如下所示:

layout-land目录下main.xml和layout内容基本一样,只是显示内容不同,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/land"
  11. />
  12. </LinearLayout>

同理layou-port目录下main.xml代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/port"
  11. />
  12. </LinearLayout>

当然我们显示的内容是在values/string.xml文件里定义的,这里加了两个值,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,ResourceDemo!</string>
  4. <stringname="app_name">ResourceDemo</string>
  5. <stringname="land">Thisislandmode.</string>
  6. <stringname="port">Thisisportmode.</string>
  7. </resources>

然后再次运行程序,效果如下:

竖屏(port)效果:

横屏(land)下效果:

通过上面例子可以得出如果我们已经定义了横屏与竖屏布局文件时,就不会在加载layout里的同名布局文件。

下面我们来讲点国际化,通常国际化我们只要在res/目录下在重新定义values-国家编号,如values-zh-rCN简体汉语,values-zh-rTW繁体,values-jp日语等。

目录结构如下图所示:

这里我只在values-zh-rCN作了改动,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,ResourceDemo!</string>
  4. <stringname="app_name">ResourceDemo</string>
  5. <stringname="land">这是横屏模式.</string>
  6. <stringname="port">这是竖屏模式.</string>
  7. </resources>

我们运行程序之前,把手机语言改成中文,在settings(设置)->language & keyboards(语言与键盘)目录下,选择简体中文,如下图所示:

最然在运行上述工程,效果如下:

这时候我们应用的显示内容就为中文了,而不去显示values/strings.xml里的内容。

Ok~今天就到这里,希望对大家特别是初学者有点帮助.Thx~

转自:http://blog.csdn.net/Android_Tutor/archive/2010/09/09/5874367.aspx

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 读取android手机流量信息
  4. android EditText设置不可写
  5. android 使用html5作布局文件: webview跟javascript交互
  6. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  7. android“设置”里的版本号
  8. Android开发环境搭建
  9. Android(安卓)Resource介绍和使用

随机推荐

  1. android 横向滚动屏幕实现(3)
  2. 更改android spinner 背景
  3. Android(安卓)SearchView介绍及搜索提示
  4. android shape
  5. android xliff字符串操作
  6. Service Manager进程启动,睡眠等待在进程p
  7. Error:Could not find com.android.tools
  8. Android中话bitmap的简单示例
  9. Android(安卓)4.x 去除输入框的蓝色边框
  10. Unknown Android(安卓)Packaging Problem