Android 资源的国际化

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

 

要使程序适应布局,则需要添加以下两个目录:layout-land 和 layout-port ,系统在进行改变的时候,将会根据这两个现在的屏幕的横竖分别读取这两种不同的布局方式,如果这当前的不存在,则会根据layout中的布局进行布局。

下面是我的的三个布局:

layout:

代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<TextView android:layout_width="fill_parent" android:id="@+id/text1"
android:layout_height
="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btn1" android:text="坚"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:id="@+id/btn2" android:text="横"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
</LinearLayout>

 

layout-land:

代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height
="wrap_content" android:text="横屏显示" />
<TextView android:layout_width="fill_parent" android:id="@+id/text1"
android:layout_height
="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btn1" android:text="横"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:id="@+id/btn2" android:text="竖"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
</LinearLayout>

 

显示效果:

layout-port:

代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height
="wrap_content" android:text="坚屏显示" />
<TextView android:layout_width="fill_parent" android:id="@+id/text1"
android:layout_height
="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btn1" android:text="横"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:id="@+id/btn2" android:text="竖"
android:layout_width
="fill_parent" android:layout_height="wrap_content">
</Button>
</LinearLayout>

 

显示效果:

下面是对内容的显示进行的国际化:

需要添加以下目录:

values-zh-rCN 此下面放置的是显示中文内容的

values-zh-rTW 此下显示繁体中文

values-jp  显示日文

一般形式为:values-国家编号

这些显示将根据操作系统的语言进行读取,如果不存在相应的语言版本,将会直接获取values中的内容:

在此中,只对中文进行了设置

values中的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ShowTask!</string>
<string name="app_name">ShowTask</string>
</resources>

 

values-zh-rCN 中的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">此程序用于显示任务!</string>
<string name="app_name">显示任务</string>
</resources>

 

显示效果如下:

 

参考文章:

http://www.android123.com.cn/androidkaifa/206.html

posted @ 2010-12-30 09:59  老码农豆豆  阅读(1062)  评论(0编辑  收藏  举报