Hello, Android之国际化

可以根据手机的语言设置或硬件配置为你的应用程序提供不同的资源。为了使你的应用程序包含可选资源,将破折号-分隔的修饰语添加到资源文件夹名称中,就可以创建平行的资源文件夹,应用程序会根据手机的设置(语言、屏幕方位、dpi等)自动选择合适的资源。

下面的代码为Hello, Android应用提供了中英文的版本:

  1. 首先,创建可选的资源文件夹

    这里,只要创建strings.xml的可选资源即可。

    将res目录下的values目录名改成values-en,strings.xml的内容如下:

    1
    2
    3
    4
    5
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Hello, Android</string>
        <string name="hello">Hello, Android!</string>
    </resources>

    新建values-zh目录,该目录下的strings.xml的内容如下:

    1
    2
    3
    4
    5
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Hello, Android</string>
        <string name="hello">你好, Android!</string>
    </resources>
  2. HelloAndroid.java文件如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    package org.gooss.android.hello;
     
    import android.app.Activity;
    import android.os.Bundle;
     
    public class HelloAndroid extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    	}
    }
  3. main.xml布局如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <?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="@string/hello"
      />
    </LinearLayout>

现在,直接按ctrl+F11运行吧。很简单。你可以通过设置模拟器的语言选项来调试Hello, Android的中英文版本。

Monitor Your Web Site 24/7 - Receive email and SMS alerts anytime your web site goes down.

Related Posts

Leave a Reply

Monitor Your Web Site 24/7 - Receive email and SMS alerts anytime your web site goes down.

About Me

About Me
Record Technical Articles at Gooss.org.

Entries (RSS) and Comments (RSS). Valid XHTML and CSS and XFN.

© Copyright 2009 山海经. All rights reserved.

Social Bookmarks

Copyright © Designed by: Free WP Templates
Food Charts |  About Breast Milk |  Organic Food