android 13 各应用语言偏好设定
各应用语言偏好设定android 13升级Android 14 遇到的各应用语言偏好设定问题
Android 13 在手机设置中新增了一个集中设置选项,用于设置各应用语言偏好设定。为了确保在搭载 Android 13 的设备上,在系统设置中可以配置应用的语言,请创建一个 locales_config XML 文件,并使用 android:localeConfig 属性将其添加到应用的清单中。省略 android:localeConfig 清单条目表明用户不应该能够在他们的手机设置中独立于系统语言来设置您的应用语言。
一、使用 android:localeConfig 将受支持的语言添加到手机设置中
若要将应用支持的语言添加到用户的手机设置中,请执行以下操作:
1.创建一个名为 res/xml/locales_config.xml 的文件,并指定您的应用的语言,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="ja"/>
<locale android:name="fr"/>
<locale android:name="en"/>
</locale-config>
2.在清单中,添加一行指向这个新文件的代码:
<manifest
...
<application
...
android:localeConfig="@xml/locales_config">
</application>
</manifest>
二、用户在系统设置中如何选择应用语言
用户可以通过新的系统设置为每个应用选择首选语言。他们可以通过以下两种方式访问这些设置:
1.通过系统设置访问
设置 > 系统 > 语言和输入法 > 应用语言 >(选择一款应用)
2.通过应用设置访问
设置 > 应用 >(选择一款应用)> 语言
三、T升级U时(Android13 升级到Android14)
当应用设置android:resizeableActivity="false",并且Activity设置android:screenOrientation=""时,设置了应用语言,更改系统语言后App会重启,页面显示语言是系统设置语言。
Intent.ACTION_LOCALE_CHANGED
If only the app locale changed, includes the following extras:
EXTRA_PACKAGE_NAME is the name of the package for which locale changed.
EXTRA_LOCALE_LIST contains locales that are currently set for specified app
1.修改方案:
在Appliction中增加
private static Locale mAppLocale;
public void onCreate() {
super.onCreate();
mAppLocale = getResources().getConfiguration().getLocales().get(0);
}
public static void setAppLocale(Locale locale) {
mAppLocale = locale;
}
public static Locale getAppLocale() {
return mAppLocale;
}
2.BaseActivity中增加
注册广播
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(mLocaleChangeReceiver, filter);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
unregisterReceiver(mLocaleChangeReceiver);
}
}
BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
LocaleList localeList = intent.getParcelableExtra(Intent.EXTRA_LOCALE_LIST);
Locale systemLocale = Resources.getSystem().getConfiguration().getLocales().get(0);
if (localeList == null) {
MyApplication.setAppLocale(systemLocale);
return;
}
if (localeList.size() == 0) {
MyApplication.setAppLocale(systemLocale);
} else {
MyApplication.setAppLocale(systemLocale);
}
String locale = Locale.getDefault().getDisplayName();
if (locale.equals(MyApplication.getAppLocale().getDisplayName())) {
recreate();
}
}
}
};
重写activity的attachBaseContext方法
@Override
protected void attachBaseContext(Context newBase) {
Resources resources = newBase.getResources();
Configuration configuration = resources.getConfiguration();
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
Locale.setDefault(MyApplication.getAppLocale());
configuration.setLocale(MyApplication.getAppLocale());
}
super.attachBaseContext(newBase);
}
更多推荐
所有评论(0)