橘子味的心
标题:11.3 Android未本地化开发

在本教程之前章节的实例中均未涉及本地化的问题,在此我们先看一下未本地化的应用程序在更改了手机的区域设置后运行效果会有什么不同。首先将手机区域设置为“zh_CN”。

新建一个 Eclipse Android Project,名为“L10NDemo”,全部使用默认设置,不修改任何代码。创建完成后,在 main.xml 文件中添加如下代码:
  1. <?xml version= "l.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center_horizontal"
  11. android:text="@string/text_a" />
  12.  
  13. <TextView
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:gravity="center_horizontal"
  17. android:text="@string/text_b" />
  18.  
  19. <Button
  20. android:id="@+id/flag_button"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_gravity="center" />
  24. </LinearLayout>
Main.xml 采用 LinearLayout 布局,分别放置了两个 TextView 和一个 Button,如图 1 所示。

默认设置的运行效果
图 1  默认设置的运行效果

Main.xml 所使用的资源文件 res/values/strings.xml 的代码如下:
  1. <resources>
  2. <string name="app_name">L10NDemo</string>
  3. <string name="text_a">这是默认的strings.xml资源文件</string>
  4. <string name="text_b">这是中国国旗</string>
  5. <string name="dialog_title">未本地化</string>
  6. <string name="dialog_text">本对话框中的内容没有本地化,相关资源来自vslues/strings.xml文件</string>
  7. </resources>
“L10NDemo”的主 Activity 为 L10NDemoActivity,MainActivity.java 的代码如下:
  1. package introduction.android.HOnDemo;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10.  
  11. import introduction.android.l10ndemo.R;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15. /**
  16. * Called when the activity is first created.
  17. */
  18. @Override
  19.  
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. Button b;
  24. (b = (Button) findViewById(R.id.flag_button)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.flag));
  25. // build dialog box to display when user clicks the flag
  26. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  27. builder.setMessage(R.string.dialog_text)
  28. .setCancelable(false)
  29. .setTitle(R.string.dialog_title)
  30. .setPositiveButton("Done", new DialogInterface.OnClickListener() {
  31. public void onClick(DialogInterface dialog, int id) {
  32. dialog.dismiss();
  33. }
  34. });
  35. final AlertDialog alert = builder.create();
  36. // set click listener on the flag to show the dialog box
  37. b.setOnClickListener(new View.OnClickListener() {
  38. public void onClick(View v) {
  39. alert.show();
  40. }
  41. });
  42. }
  43. }

L10NDemoActivity 为 main.xml 中的 Button 设置了一幅图像,是 R.drawable.flag 指向的图像文件。

当用户单击 Button 时,即可弹出一个有 Done 按钮的 AlertDialog,显示 R.string.dialog_text 指向的内容,运行效果如图 2 所示。
按钮修改的运行效果
图 2  按钮修改的运行效果

然后将手机区域设置修改为“en_US”,即美式英语。再次运行该实例,运行效果如图 3 所示。再将手机区域设置为其他区域,运行效果不变。
区域修改的运行效果
图 3   区域修改的运行效果

可见实例“L10NDemo”在不同的区域设置下,运行效果完全相同。这是因为 Android 系统对于未实现本地化的应用程序,均使用默认的资源文件,无论当前手机设备被设置为任何地区,应用程序的运行效果都相同。