甚至在android10之前,我就一直在努力解决三星设备上的动态区域设置更改问题。
但当时除了你所做的,
我最终通过以下方式通过资源标识符检索所有字符串:
public static String getStringByIdentifier(final Context context, final String stringResIdName, final boolean forceRefresh) {
final Resources res = getResources(context, forceRefresh);
String result;
try {
result = res.getString(res.getIdentifier(stringResIdName, "string",
context.getPackageName()));
} catch (final Resources.NotFoundException e) {
result = stringResIdName; // TODO or here you may throw an Exception and handle it accordingly.
}
return result;
}
public static String getStringByIdentifier(final Context context, final String stringResIdName) {
return getStringByIdentifier(context, stringResIdName, false);
}
private static Resources getResources(final Context context, final boolean refreshLocale) {
if (!refreshLocale) {
return context.getResources();
} else {
final Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(Locale.getDefault());
return context.createConfigurationContext(configuration).getResources();
}
}
textView.setText(AndroidUtils.getStringByIdentifier(context, "string_res_name"));
其中对应的字符串资源是:
<string name="string_res_name">Some string</string>