- 引入BaseActivity统一处理多语言切换,所有Activity继承BaseActivity - 添加LocaleHelper工具类实现应用内语言切换功能 - 重构设置页面,移除Google Tasks同步相关功能,新增字体大小、随机背景色和多语言设置 - 优化侧边栏布局,将退出登录按钮移至底部并移除登录按钮 - 修复字体大小设置类型转换问题,支持字符串存储 - 新增笔记导出功能集成到设置页面 - 完善中英文翻译字符串资源,统一界面文本显示 - 更新应用版本至1.1.0baoerjun_branch
parent
16e249516a
commit
86d39b7a95
@ -0,0 +1,92 @@
|
||||
package net.micode.notes.tool;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocaleHelper {
|
||||
|
||||
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
|
||||
|
||||
public static Context onAttach(Context context) {
|
||||
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
|
||||
return setLocale(context, lang);
|
||||
}
|
||||
|
||||
public static Context onAttach(Context context, String defaultLanguage) {
|
||||
String lang = getPersistedData(context, defaultLanguage);
|
||||
return setLocale(context, lang);
|
||||
}
|
||||
|
||||
public static String getLanguage(Context context) {
|
||||
return getPersistedData(context, Locale.getDefault().getLanguage());
|
||||
}
|
||||
|
||||
public static Context setLocale(Context context, String language) {
|
||||
persist(context, language);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
return updateResources(context, language);
|
||||
}
|
||||
|
||||
return updateResourcesLegacy(context, language);
|
||||
}
|
||||
|
||||
private static String getPersistedData(Context context, String defaultLanguage) {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
// 默认语言改为中文 (zh-CN)
|
||||
return preferences.getString(SELECTED_LANGUAGE, "zh-CN");
|
||||
}
|
||||
|
||||
private static void persist(Context context, String language) {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.putString(SELECTED_LANGUAGE, language);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private static Context updateResources(Context context, String language) {
|
||||
Locale locale = getLocale(language);
|
||||
Locale.setDefault(locale);
|
||||
|
||||
Configuration configuration = context.getResources().getConfiguration();
|
||||
configuration.setLocale(locale);
|
||||
configuration.setLayoutDirection(locale);
|
||||
|
||||
return context.createConfigurationContext(configuration);
|
||||
}
|
||||
|
||||
private static Context updateResourcesLegacy(Context context, String language) {
|
||||
Locale locale = getLocale(language);
|
||||
Locale.setDefault(locale);
|
||||
|
||||
Resources resources = context.getResources();
|
||||
Configuration configuration = resources.getConfiguration();
|
||||
configuration.locale = locale;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
configuration.setLayoutDirection(locale);
|
||||
}
|
||||
|
||||
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private static Locale getLocale(String language) {
|
||||
if (language.equals("zh-CN")) {
|
||||
return Locale.SIMPLIFIED_CHINESE;
|
||||
} else if (language.equals("zh-TW")) {
|
||||
return Locale.TRADITIONAL_CHINESE;
|
||||
} else if (language.equals("en")) {
|
||||
return Locale.ENGLISH;
|
||||
} else if (language.equals("system")) {
|
||||
return Locale.getDefault();
|
||||
}
|
||||
return new Locale(language);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package net.micode.notes.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import net.micode.notes.tool.LocaleHelper;
|
||||
|
||||
public class BaseActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void attachBaseContext(Context newBase) {
|
||||
super.attachBaseContext(LocaleHelper.onAttach(newBase));
|
||||
}
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!-- Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||
|
||||
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.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="50dip"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:text="@string/preferences_add_account" />
|
||||
</LinearLayout>
|
||||
Loading…
Reference in new issue