安卓国际化,应用内切换语言

本文介绍了一种在安卓应用中实现多语言支持的方法,通过设置不同的资源文件夹来对应各种语言环境,包括如何切换语言及更新UI显示。

很多应用开发中,我们需要做多语言版本,安卓在这方面做的很好,下面就写一个简单的例子:


一、项目目录结构


这里对几个关键点进行说明下:

drawable-hdpi 为我们默认的图片存放目录

drawable-en-hdpi 为英文版本对应的图片存放目录,当然ldpi\mdpi\xhdpi 也一样,如果需要,分别按这样的规则创建目录即可。

values 为默认的配置文件目录

values-en 为英文版本的配置文件目录


values 目录下的strings.xml 内容为:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">语言切换</string>  
  4.     <string name="content">大家好,我叫单红宇。</string>  
  5.     <string name="content2">AAAAA</string>  
  6. </resources>  

values-en 目录下的strings.xml 内容为:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">Language Switch</string>  
  5.     <string name="content">Hello everyone, My name is SHANHY.</string>  
  6.   
  7. </resources>  

我故意在第一个strings.xml中放了content2,在第二个英文的里面没有放,是为了要说明不同语言的配置文件内容是不需要一一对应的,我们只需要在需要多语言的配置放到按规则命名的文件夹下即可。

drawable-en-hdpi 和 drawable-hdpi 下面放了2个名称相同,内容不同的图片,也是为了要说明,图片也只一样支持多语言的。


下面为MainActivity 代码:

  1. package com.shanhy.example.spinner;  
  2.   
  3. import java.util.Locale;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.res.Configuration;  
  7. import android.content.res.Resources;  
  8. import android.os.Bundle;  
  9. import android.util.DisplayMetrics;  
  10. import android.view.View;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.     }  
  19.   
  20.     /** 
  21.      * 英语 
  22.      *  
  23.      * @param v 
  24.      * @author SHANHY 
  25.      * @date 2015年8月28日 
  26.      */  
  27.     public void switchEnglish(View v) {  
  28.         switchLanguage(Locale.ENGLISH);  
  29.     }  
  30.   
  31.     /** 
  32.      * 中文 
  33.      *  
  34.      * @param v 
  35.      * @author SHANHY 
  36.      * @date 2015年8月28日 
  37.      */  
  38.     public void switchChinese(View v) {  
  39.         switchLanguage(Locale.CHINESE);  
  40.     }  
  41.   
  42.     /** 
  43.      * 切换语言 
  44.      *  
  45.      * @param locale 
  46.      * @author SHANHY 
  47.      * @date 2015年8月28日 
  48.      */  
  49.     private void switchLanguage(Locale locale) {  
  50.         Resources res = getResources();  
  51.         Configuration config = res.getConfiguration();  
  52.         DisplayMetrics dm = res.getDisplayMetrics();  
  53.         Locale currentLocal = config.locale;  
  54.         config.locale = locale;  
  55.         res.updateConfiguration(config, dm);  
  56.   
  57.         // 如果切换了语言  
  58.         if (!currentLocal.equals(config.locale)) {  
  59.             // 这里需要重新刷新当前页面中使用到的资源  
  60.             onCreate(null);  
  61.         }  
  62.     }  
  63. }  


main.xml 和 AndroidManifest.xml 和我们平常开发是一样的,没有任何特殊的地方。

main.xml

  1. <?xml version="1.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:background="@android:color/white"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:textColor="@android:color/black"  
  12.         android:text="English" android:onClick="switchEnglish"/>  
  13.   
  14.     <Button  
  15.         android:layout_width="wrap_content"  
  16.         android:textColor="@android:color/black"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="中文" android:onClick="switchChinese"/>  
  19.   
  20.     <TextView  
  21.         android:id="@+id/SpinnerResult"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_gravity="center_horizontal"  
  25.         android:gravity="center"  
  26.         android:lineSpacingExtra="10dp"  
  27.         android:text="@string/content"  
  28.         android:textColor="@android:color/black"  
  29.         android:textSize="10pt"  
  30.         android:textStyle="bold" >  
  31.     </TextView>  
  32.           
  33.     <TextView  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_gravity="center_horizontal"  
  37.         android:gravity="center"  
  38.         android:lineSpacingExtra="10dp"  
  39.         android:text="@string/content2"  
  40.         android:textColor="@android:color/black"  
  41.         android:textSize="10pt"  
  42.         android:textStyle="bold" >  
  43.     </TextView>  
  44.   
  45.     <ImageView  
  46.         android:id="@+id/img"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:layout_gravity="center_horizontal"  
  50.         android:background="@drawable/testimg" />  
  51.   
  52. </LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值