动态设置多语言
效果图:
需求:在设置中添加一个可以设置语言的需求,点击选中的语言,就可以切换显示。
demo代码:
APP类:app初始化的时候获取保存的语言(没有就系统默认),设置语言。
public class APP extends Application {
private Object locale;
@Override
public void onCreate() {
super.onCreate();
//设置语言
setLanguage();
}
private void setLanguage() {
Locale locale = getLocale();
Configuration configuration = getResources().getConfiguration();
DisplayMetrics metrics = getResources().getDisplayMetrics();
if (locale != null) {
configuration.locale = locale;
getResources().updateConfiguration(configuration, metrics);
} else {
configuration.locale = Locale.getDefault();
getResources().updateConfiguration(configuration, metrics);
}
}
public Locale getLocale() {
File file = new File(getFilesDir(), "locale");
if (!file.exists()) {
return null;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
return (Locale) objectInputStream.readObject();
} catch (Exception e) {
} finally {
try {
if (objectInputStream != null) {
objectInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
SettingActivity页面:
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_en;
private TextView tv_zh;
private TextView tv_ja;
private TextView tv_ar;
private TextView tv_defalut;
private Configuration configuration;
private DisplayMetrics displayMetrics;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar == null) return;
toolbar.setTitle(getString(R.string.app_name));
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
tv_defalut = (TextView) findViewById(R.id.defalut);
tv_en = (TextView) findViewById(R.id.en);
tv_zh = (TextView) findViewById(R.id.zh);
tv_ja = (TextView) findViewById(R.id.ja);
tv_ar = (TextView) findViewById(R.id.ar);
configuration = getResources().getConfiguration();
displayMetrics = getResources().getDisplayMetrics();
tv_defalut.setOnClickListener(this);
tv_en.setOnClickListener(this);
tv_zh.setOnClickListener(this);
tv_ja.setOnClickListener(this);
tv_ar.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.defalut:
configuration.locale = Locale.getDefault();
break;
case R.id.en:
configuration.locale = Locale.ENGLISH;
break;
case R.id.zh:
configuration.locale = Locale.SIMPLIFIED_CHINESE;
break;
case R.id.ja:
configuration.locale = Locale.JAPANESE;
break;
case R.id.ar:
configuration.locale = new Locale("ar", "");
break;
default:
configuration.locale = Locale.getDefault();
break;
}
//切换语言时,同时改变水平布局的方向
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(configuration.locale);
}
saveLocale(configuration.locale);
getResources().updateConfiguration(configuration, displayMetrics);
reloadApp();
}
private void saveLocale(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
File file = new File(getFilesDir(), "locale");
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
objectOutputStream.writeObject(locale);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void reloadApp() {
Intent intent = new Intent(SettingActivity.this, LauncherActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
/**
* 设置singleTask启动模式
* 1.配置文件中设置launchMode="singleTask"
* 2.页面跳转时setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
*
* singleTask启动模式:当activity设置了该启动模式,再次跳转启动时,会将任务栈中的其他activity销毁,只让自己独自在栈中
*/
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
/**
* 设置singleTop启动模式
* 1.配置文件设置launchMode="singleTop"效果一样
* 2.页面跳转时setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
*
* singleTop启动模式:当activity设置了该启动模式,再次跳转启动时,会将任务栈中,在该activity之上的activity销毁,保持该activityyi处于栈顶状态。
*/
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
strings.xml内容:
默认/en
<string name="app_name">Multi_language</string>
<string name="title">Hello world</string>
阿拉伯语/ar
<string name="app_name">متعدد اللغات</string>
<string name="title">مرحبا العالم</string>
日语/ja
<string name="app_name">マルチ言語</string>
<string name="title">こんにちは世界</string>
汉语/zh
<string name="app_name">多语言</string>
<string name="title">你好,世界</string>
主要是通过activity的启动模式,重新启动app,做初始化操作,语言就设置成功了。