近期在做一个背单词APP来作为毕业设计,在app里面想设计一个切换语言为英语的功能,记录一下本次实现的效果以及步骤。
文章目录
实现效果中文模式英文模式
实现原理实现步骤1、创建相关的value文件夹和string.xml文件2、 在对应的string文件中选择合适的翻译3、在对应的view中使用该string字段4、修改资源文件对应的Locale属性5、 重启Activity6、设置重启后恢复SP储存的属性
最终效果图
实现效果
中文模式
英文模式
PS: 请不要吐槽我的垃圾英语翻译水平,纯机翻哈哈哈
实现原理
在Android当中针对不同的ConfigChanges会有不同的响应事件,对于手机语言状态的改变会引起对应的res / values的目录的使用,例如对于中文模式使用的就是默认的文件夹: 对于英语使用的就是values_en的文件夹下的xml文件。
语言对应的文件夹名字可以通过Android studio来直接创建对应的Resource Dictionnary: 再自己选择对应语言,这里以英语为例: 其他语言文件夹同理。
然后在对应的语言文件夹下的String.xml文件中翻译对应的字符串即可。
对于系统语言级别的切换App会自动进行响应切换为对应的语言,若对应的语言没有相关的文件夹则是使用默认的语言文件夹,若有则使用对应的语言文件夹。
对于app内部自己要进行语言的切换则是需要自己通过代码手动来进行切换对应的ConfigChanges事件。
实现步骤
1、创建相关的value文件夹和string.xml文件
2、 在对应的string文件中选择合适的翻译
values文件夹下的string文件(默认):
values_en文件夹下的string文件(英文):
3、在对应的view中使用该string字段
例:
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:background="@color/colorBgLittleGrey" tools:context=".fragment.Fragment_function"> android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="16dp" android:text="@string/function_expore" //这里来进行使用!!!!! android:textColor="@color/colorTextBlackNomal" android:textSize="@dimen/appbar_text_size" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/function_grid_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:horizontalSpacing="16dp" android:numColumns="2" android:verticalSpacing="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView6" />
对于为了适应手机语言切换来适配的进行到这一步就可以了。 对于APP内部主动进行修改的还要进行下面的步骤。
4、修改资源文件对应的Locale属性
在要修改语言的相应位置中加上相关代码:
switchToEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// 获得res资源对象
Resources resources = getActivity().getResources();
// 获得屏幕参数,主要是用来下面的切换
DisplayMetrics metrics = resources.getDisplayMetrics();
// 获得配置对象
Configuration config = resources.getConfiguration();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
config.locale = Locale.ENGLISH;
locale = config.locale;
Toast.makeText(getContext(), "切换为英语模式!", Toast.LENGTH_SHORT).show();
} else {
config.locale = Locale.CHINESE;
Toast.makeText(getContext(), "切换为中文模式!", Toast.LENGTH_SHORT).show();
}
if (locale == Locale.ENGLISH) {
updateActivity("English");
} else {
updateActivity("Chinese");
}
}
});
在以上代码主要做的事情就是切换了config.locale的相关属性,详细见其源码。
5、 重启Activity
在修改完之后要重启Activity的时候才会重新载入相关属性,因此我们要做的第二步就是保存当前属性,重启Actiivity。
详细代码就是updateActivity方法:
public void updateActivity(String language) {
//在本地SP中保存当前选择的语言
SharedPreferencesUtils.setParem(getContext(), "languege", language);
Locale local = this.locale;
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = local;
res.updateConfiguration(conf, dm);
//设置好选择的语言以后,需要清除任务栈中的所有activity,打开首页,确保以后进入的页面都是当前选择的语言
Intent intent = new Intent(getActivity(), MainActivity.class);
//清空任务栈确保当前打开activit为前台任务栈栈顶
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
getActivity().finish();
}
}
6、设置重启后恢复SP储存的属性
这里我参考网上的代码,在自定义Application当中来做:
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
setCurrLanguageMode();
}
//设置当前APP的语言模式
private void setCurrLanguageMode() {
String language = (String) SharedPreferencesUtils.getParem(this, "languege", "");
Locale local = LanguageUtils.getLocale(language);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = local;
res.updateConfiguration(conf, dm);
}
public void init() {
//LitePal.initialize(this);
}
}
最终效果图
结束。
参考文章: https://www.jianshu.com/p/ef749f54faba在这里插入图片描述

