7.0 以下
,需要在 build.gradle
文件中加入allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
7.0 及以上
,则需要在 settings.gradle
文件中加入dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
build.gradle
文件中加入远程依赖dependencies {
// 语种切换框架:https://github.com/getActivity/MultiLanguages
implementation 'com.github.getActivity:MultiLanguages:8.0'
}
public final class XxxApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化语种切换框架
MultiLanguages.init(this);
}
}
@Override
protected void attachBaseContext(Context base) {
// 绑定语种
super.attachBaseContext(MultiLanguages.attach(base));
}
@Override
protected void attachBaseContext(Context newBase) {
// 绑定语种
super.attachBaseContext(MultiLanguages.attach(newBase));
}
// 设置当前的语种(返回 true 表示需要重启 App)
MultiLanguages.setAppLanguage(Context context, Locale locale);
// 获取当前的语种
MultiLanguages.getAppLanguage();
// 跟随系统语种(返回 true 表示需要重启 App)
MultiLanguages.clearAppLanguage(Context context);
// 获取系统的语种
MultiLanguages.getSystemLanguage();
// 是否跟随系统的语种
MultiLanguages.isSystemLanguage();
// 对比两个语言是否是同一个语种(比如:中文的简体和繁体,英语的美式和英式)
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
// 对比两个语言是否是同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
// 获取某个语种下的 String
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
// 获取某个语种下的 Resources 对象
MultiLanguages.getLanguageResources(Context context, Locale locale);
// 更新 Context 的语种
MultiLanguages.updateAppLanguage(Context context);
// 更新 Resources 的语种
MultiLanguages.updateAppLanguage(Resources resources);
// 设置语种变化监听器
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
@Override
public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "监听到应用切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
}
@Override
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale + ",是否跟随系统:" + MultiLanguages.isSystemLanguage());
}
});
@Override
public void onClick(View v) {
// 是否需要重启
boolean restart;
switch (v.getId()) {
// 跟随系统
case R.id.btn_language_auto:
restart = MultiLanguages.clearAppLanguage(this);
break;
// 简体中文
case R.id.btn_language_cn:
restart = MultiLanguages.setAppLanguage(this, Locale.CHINA);
break;
// 繁体中文
case R.id.btn_language_tw:
restart = MultiLanguages.setAppLanguage(this, Locale.TAIWAN);
break;
// 英语
case R.id.btn_language_en:
restart = MultiLanguages.setAppLanguage(this, Locale.ENGLISH);
break;
default:
restart = false;
break;
}
if (restart) {
// 我们可以充分运用 Activity 跳转动画,在跳转的时候设置一个渐变的效果
startActivity(new Intent(this, LanguageActivity.class));
overridePendingTransition(R.anim.activity_alpha_in, R.anim.activity_alpha_out);
finish();
}
}
public final class LanguagesWebView extends WebView {
public LanguagesWebView(@NonNull Context context) {
this(context, null);
}
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 修复 WebView 初始化时会修改 Activity 语种配置的问题
MultiLanguages.updateAppLanguage(context);
}
}
我先问大家一个问题,生米煮成熟饭了,怎么从熟饭变成生米?这显然是不现实的,退一万步讲,假设框架能做到,文字和图片都能自动跟随语种的变化而变化,那么通过接口请求的数据又怎么切换语种?是不是得重新请求?如果是列表数据是不是得从第 1 页开始请求?再问大家一个问题,还有语种切换是一个常用动作吗?我相信大家此时心里已经有了答案。
所以并不是做不到不用重启的效果,而是没有那个必要(切语种不是常用动作),并且存在一定的硬伤(虽然 UI 层不用动,但是数据层还是要重新请求)。
安卓技术中台:AndroidProject
安卓技术中台 Kt 版:AndroidProject-Kotlin
权限框架:XXPermissions
吐司框架:ToastUtils
网络框架:EasyHttp
标题栏框架:TitleBar
悬浮窗框架:XToast
Shape 框架:ShapeView
Gson 解析容错:GsonFactory
日志查看框架:Logcat
Android 版本适配:AndroidVersionAdapter
Android 代码规范:AndroidCodeStandard
Studio 精品插件:StudioPlugins
表情包大集合:EmojiPackage
省市区 Json 数据:ProvinceJson
Copyright 2019 Huang JinQun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。