获取值在Android应用程序中以不同语言显示 - 本地化

时间:2013-12-10 09:18:58

标签: android xml user-interface android-activity

在我的 android 应用程序的活动中,我不得不使用三种不同语言的字符串,以便在启动应用时选择首选语言。我在values文件夹中使用维度XML文件为不同的屏幕尺寸设计了android UI 。例如,使用

android:textSize="@dimen/normalText"

在文本视图中设置尺寸

<TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Last Login Date    : "
            android:textColor="@color/white"
            android:textSize="@dimen/normalText"
            android:textStyle="bold" />

我可以使用这种方法来使用多种语言启用情况。我需要在应用程序启动时选择一种语言,并使用该语言加载所有UI元素。每当语言在另一次启动时发生变化时,应用程序必须在UI中显示相关语言元素。这是可能的。请帮助我使用有用的代码段。谢谢!

2 个答案:

答案 0 :(得分:2)

根据您的语言列表在res中创建values文件夹:

1. values-es
2. values-ta
3. values-fr

并设置特定语言文件夹的资源值,然后使用以下链接:

http://android.programmerguru.com/android-localization-at-runtime/

编辑:

下面的代码是针对所有语言的strings.xml文件:

英语(strings.xml into values-en):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

西班牙语(strings.xml into values-es):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mi Aplicación</string>
    <string name="hello_world">Hola Mundo!</string>
</resources>

法语(strings.xml到values-fr):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mon Application</string>
    <string name="hello_world">Bonjour le monde !</string>
</resources>

答案 1 :(得分:1)

如果用户更改语言,我会重新启动应用程序:

Intent i = new Intent(YourApplication.getInstance(), StartAcitivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(YourApplication.getInstance().getBaseContext(), 0,  i, Intent.FLAG_ACTIVITY_CLEAR_TOP);

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, intent);

System.exit(2);

但是,如果有人提供更优雅的解决方案,那会很有吸引力。