在我的Android应用程序中,我有一个屏幕,该屏幕根据以下规则将多个项目显示到RecyclerView
中:
为了让系统根据设备方向选择正确的值(3或4),我将该值存储在资源文件夹中:
<integer name="videos_recycler_span">3</integer>
被放入values-sw600dp
<integer name="videos_recycler_span">4</integer>
被放入values-sw600dp-land
我还覆盖了attachBaseContext
的{{1}}方法,因为我想独立于设备的语言来管理应用程序的语言。
这里是方法:
Activity
这里是override fun attachBaseContext(newBase: Context)
{
val appCountry = DataContainer.INSTANCE.getUICountry(newBase)
val appLang = DataContainer.INSTANCE.getUILang(newBase)
val appLocale = Locale(appLang, appCountry)
val wrapped = LangContextWrapper.wrap(newBase, appLocale)
super.attachBaseContext(wrapped)
}
类:
LangContextWrapper
然后从我的片段中,用class LangContextWrapper(base: Context)
: ContextWrapper(base)
{
companion object
{
fun wrap(context: Context, newLocale: Locale): ContextWrapper
{
val res = context.resources
val configuration = res.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration.locales = localeList
}
else
{
configuration.setLocale(newLocale)
val dm = res.displayMetrics
res.updateConfiguration(configuration, dm)
}
configuration.setLayoutDirection(newLocale)
return ContextWrapper(context.createConfigurationContext(configuration))
}
}
}
检索所需的值。
如果我在启动屏幕之前更改方向,则效果很好,但是在至少两台设备上,资源是我留在屏幕上并进行旋转。
设备是: -Android 7.1.1上的平板电脑三星SM-T550 -平板电脑Samsung SM-T285(Android 5.1.1版)
我没有其他用于测试的物理平板电脑,但是例如,我没有在模拟器上重现该问题。
如果我不覆盖resources.getInteger(R.integer.videos_recycler_span)
,它将再次起作用。
所以我猜我的attachBaseContext
类在某些三星设备上有问题吗?有人可以知道这个问题是什么以及如何解决吗?
答案 0 :(得分:0)
根据我的测试,这是Android的问题。我可以在使用API 24的仿真器上重现该问题。但是不能在使用API 28的仿真器上重现该问题。