很多应用开发中,我们需要做多语言版本,安卓在这方面做的很好,下面就写一个简单的例子:
一、项目目录结构
这里对几个关键点进行说明下:
drawable-hdpi 为我们默认的图片存放目录
drawable-en-hdpi 为英文版本对应的图片存放目录,当然ldpi\mdpi\xhdpi 也一样,如果需要,分别按这样的规则创建目录即可。
values 为默认的配置文件目录
values-en 为英文版本的配置文件目录
values 目录下的strings.xml 内容为:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">语言切换</string> <string name="content">大家好,我叫单红宇。</string> <string name="content2">AAAAA</string> </resources>
values-en 目录下的strings.xml 内容为:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Language Switch</string> <string name="content">Hello everyone, My name is SHANHY.</string> </resources>
我故意在第一个strings.xml中放了content2,在第二个英文的里面没有放,是为了要说明不同语言的配置文件内容是不需要一一对应的,我们只需要在需要多语言的配置放到按规则命名的文件夹下即可。
drawable-en-hdpi 和 drawable-hdpi 下面放了2个名称相同,内容不同的图片,也是为了要说明,图片也只一样支持多语言的。
下面为MainActivity 代码:
package com.shanhy.example.spinner; import java.util.Locale; import android.app.Activity; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } /** * 英语 * * @param v * @author SHANHY * @date 2015年8月28日 */ public void switchEnglish(View v) { switchLanguage(Locale.ENGLISH); } /** * 中文 * * @param v * @author SHANHY * @date 2015年8月28日 */ public void switchChinese(View v) { switchLanguage(Locale.CHINESE); } /** * 切换语言 * * @param locale * @author SHANHY * @date 2015年8月28日 */ private void switchLanguage(Locale locale) { Resources res = getResources(); Configuration config = res.getConfiguration(); DisplayMetrics dm = res.getDisplayMetrics(); Locale currentLocal = config.locale; config.locale = locale; res.updateConfiguration(config, dm); // 如果切换了语言 if (!currentLocal.equals(config.locale)) { // 这里需要重新刷新当前页面中使用到的资源 onCreate(null); } } }
main.xml 和 AndroidManifest.xml 和我们平常开发是一样的,没有任何特殊的地方。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="English" android:onClick="switchEnglish"/> <Button android:layout_width="wrap_content" android:textColor="@android:color/black" android:layout_height="wrap_content" android:text="中文" android:onClick="switchChinese"/> <TextView android:id="@+id/SpinnerResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:lineSpacingExtra="10dp" android:text="@string/content" android:textColor="@android:color/black" android:textSize="10pt" android:textStyle="bold" > </TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:lineSpacingExtra="10dp" android:text="@string/content2" android:textColor="@android:color/black" android:textSize="10pt" android:textStyle="bold" > </TextView> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="@drawable/testimg" /> </LinearLayout>