使用系统语言实现国际化还是比较简单的,主要的的也就是创建各种不同的values-之类的东西,其实很简单 可以直接去搜一下就能找到 比如按照下面的链接完全可以去实现 (http://jingyan.baidu.com/article/9f63fb91a90ca3c8410f0e62.html)
这里我要说的就是在 系统自带的那个values中的String可以使用默认的英文了,没有必要非得去写一个values-en
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">wangqiu</string> </resources>还有一个就是中文下的values-zh-rCN的strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">玩笑</string> </resources>接下来就是去写我们的布局文件了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/h" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:onClick="change" /> </LinearLayout>
import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.preference.PreferenceManager; import android.util.DisplayMetrics; import java.util.Locale; public class LanguageSettingUtil { //单例模式- private static LanguageSettingUtil instance; private Context context; //存储当前系统的language设置- private String defaultLanguage; //存储当前系统Locale- private Locale defaultLocale; public final static String ENGLISH = "en"; public final static String CHINESE = "zh"; private LanguageSettingUtil(Context paramContext) { //得到系统语言- Locale localLocale = Locale.getDefault(); this.defaultLocale = localLocale; //保存系统语言到defaultLanguage String str = this.defaultLocale.getLanguage(); this.defaultLanguage = str; this.context = paramContext; } //检验自身是否被创建- public static LanguageSettingUtil get() { if (instance == null) throw new IllegalStateException( "language setting not initialized yet"); return instance; } //初始化- public static void init(Context paramContext) { if (instance == null) { instance = new LanguageSettingUtil(paramContext); } } // 创建Configuration- private Configuration getUpdatedLocaleConfig(String paramString) { Configuration localConfiguration = context.getResources() .getConfiguration(); Locale localLocale = getLocale(paramString); localConfiguration.locale = localLocale; return localConfiguration; } //得到APP配置文件目前的语言设置- public String getLanguage() { SharedPreferences localSharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.context); //如果当前程序没有设置language属性就返回系统语言,如果有,就返回以前的- return localSharedPreferences.getString("language", this.defaultLanguage); } //如果配置文件中没有语言设置- public Locale getLocale() { String str = getLanguage(); return getLocale(str); } //创建新Locale并覆盖原Locale- public Locale getLocale(String paramString) { Locale localLocale = new Locale(paramString); Locale.setDefault(localLocale); return localLocale; } //刷新显示配置- public void refreshLanguage() { String str = getLanguage(); Resources localResources = this.context.getResources(); if (!localResources.getConfiguration().locale.getLanguage().equals(str)) { Configuration localConfiguration = getUpdatedLocaleConfig(str); // A structure describing general information about a display, such // as its size, density, and font scaling. DisplayMetrics localDisplayMetrics = localResources .getDisplayMetrics(); localResources.updateConfiguration(localConfiguration, localDisplayMetrics); } } //设置系统语言- public void saveLanguage(String paramString) { PreferenceManager.getDefaultSharedPreferences(this.context).edit() .putString("language", paramString).commit(); } //保存系统的语言设置到SharedPreferences- public void saveSystemLanguage() { PreferenceManager.getDefaultSharedPreferences(this.context).edit() .putString("PreSysLanguage", this.defaultLanguage).commit(); } public void checkSysChanged(String cuerSysLanguage) { //如果系统语言设置发生变化- if (!cuerSysLanguage.equals(PreferenceManager .getDefaultSharedPreferences(this.context).getString( "PreSysLanguage", "zh"))) { //如果系统保存了this对象,就在这里修改defaultLanguage的值为当前系统语言cuerSysLanguage this.defaultLanguage = cuerSysLanguage; saveLanguage(cuerSysLanguage); saveSystemLanguage(); } } }最后就是在activity中的使用了:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private LanguageSettingUtil languageSetting; private TextView h,w; private Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); h = (TextView) findViewById(R.id.h); b = (Button) findViewById(R.id.btn); LanguageSettingUtil.init(this);//初始化 languageSetting = LanguageSettingUtil.get();//检查是否初始化 } public void change(View view){ languageSetting.saveLanguage("en");//设置为"en"语言(英语),cn,是中文 LanguageSettingUtil.get().refreshLanguage();// 刷新 h.setText(R.string.app_name);//重新加载一次,不然界面更新不了 b.setText(R.string.app_name);//同上。当然 也可以使用recreate();代替这两行代码不过会出现闪屏,用户的体验不好 } }
private Configuration config; private DisplayMetrics dm; private Resources resources
resources = getResources();// 获得res资源对象 config = resources.getConfiguration();// 获得设置对象 dm = resources.getDisplayMetrics();