Android res.getIdentifier()仅当上下文用特定语言包装时才返回零
在我的活动中,我使用以下行查找需要充气的布局的id:Android res.getIdentifier()仅当上下文用特定语言包装时才返回零,android,Android,在我的活动中,我使用以下行查找需要充气的布局的id: int layoutId = res.getIdentifier("name_of_layout_that_does_exist", "layout", context.getPackageName()); 这工作正常,返回相关布局的id,工作正常后的一切。。。除非应用程序的语言设置为土耳其语!(tr) 我通过活动定义中的以下覆盖设置活动所使用的语言: @Override protected void attachBaseContext(C
int layoutId = res.getIdentifier("name_of_layout_that_does_exist", "layout", context.getPackageName());
这工作正常,返回相关布局的id,工作正常后的一切。。。除非应用程序的语言设置为土耳其语!(tr
)
我通过活动定义中的以下覆盖设置活动所使用的语言:
@Override
protected void attachBaseContext(Context baseContext) {
String appLocale = MyFunctions.getAppLocale(baseContext); // this is "tr"
super.attachBaseContext(MyContextWrapper.wrap(baseContext, appLocale));
}
其中MyContextWrapper
是:
public class MyContextWrapper extends ContextWrapper {
private static final String TAG = "Meteogram_ContextWrappr";
public MyContextWrapper(Context base) {
super(base);
}
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale = myGetLocale(config);
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setLocale(config, locale);
} else {
setLocaleLegacy(config, locale);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
return new MyContextWrapper(context);
}
@SuppressWarnings("deprecation")
public static Locale getLocaleLegacy(Configuration config){
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static Locale getLocale(Configuration config){
return config.getLocales().get(0);
}
public static Locale myGetLocale(Context context) {
Configuration config = context.getResources().getConfiguration();
return myGetLocale(config);
}
public static Locale myGetLocale(Configuration config) {
Locale sysLocale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = getLocale(config);
} else {
sysLocale = getLocaleLegacy(config);
}
return sysLocale;
}
@SuppressWarnings("deprecation")
public static void setLocaleLegacy(Configuration config, Locale locale){
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static void setLocale(Configuration config, Locale locale){
config.setLocale(locale);
}
}
这一切都适用于任何其他语言(我的应用程序中也有一些),甚至对于土耳其语,其他等效版面的版面id也会很好地返回。。。这只是一种布局,而且只适合土耳其语
我被难住了。你在评论中说
字符串layoutName=“layout”+tag.replaceAll(([A-Z]),“\u$1”)。toLowerCase()
。您应该知道toLowerCase()
和toUpperCase()
会对土耳其语中的i
字符产生“奇怪”的效果。我99%确信这是问题的根源
您可以通过将
Locale
参数传递给toLowerCase()
调用来解决此问题。。。可能需要Locale.ROOT.我有一个(弱)理论,但它取决于布局名称的实际值和包名称的实际值。你能提供这些吗?@BenP。好奇你在想什么。土耳其语的“我”问题?当某件事失败时,我总是这么想。是的,我是我的第一个想法。但我愿意相信这也是另一个铭文。嗯,你可能发现了什么。。。有一点模式,但不完全。。。有关的布局是layout\u header\u信息
,但我现在发现它在layout\u app\u信息
中也失败了。但是,对于layout\u index\u bar
和layout\u time\u设置
,这是可以的。不确定它是否相关,但我根据带有String layoutName=“layout”+tag.replaceAll(([a-Z]),“$1”).toLowerCase()
的“标记”确定了布局名称,这样layou headerıinformation
来自标记headerInformation
。因此它可能是大写“I
”的转换就像在标题信息
到版面标题信息
中一样,但是当从指示栏
转换到版面索引栏
时是可以的,听起来很有希望。。。我刚出去,但今天晚些时候会试试。事实上我已经等不及了。。。试过了,效果很好。。。非常感谢你!为什么对Locale.ROOT
不感兴趣?Locale.ROOT
尽可能接近“通用”或“普通”语言环境。它没有任何特殊的规则,当你操作代码而不是真正的人类语言时,这种愚蠢是有价值的。