gpt4 book ai didi

API 27 上的 Android 不同风格

转载 作者:太空狗 更新时间:2023-10-29 13:02:49 26 4
gpt4 key购买 nike

为应用程序定义了不同的样式和字符串。在 8 之前的 Android 上一切正常,但在 Android 8 上样式加载不正确。

注意:字符串很好。它只是风格。

res
values
styles.xml
strings.xml
values-fr
styles.xml
strings.xml
values-v21
styles.xml

我这样更改了应用程序语言环境:

public static Context changeAppLocale(String lang, Context c) {

Locale locale = new Locale(lang);
Resources resources = c.getResources();
Configuration config = new Configuration(resources.getConfiguration());
Locale.setDefault(locale);
if(Build.VERSION.SDK_INT > 16) {
config.setLayoutDirection(locale);
}
if(Build.VERSION.SDK_INT > 24){
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocale(locale);
config.setLocales(localeList);
c = c.createConfigurationContext(config);
}
else{
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
return c;
}

然后我像这样在 Activity 中应用它:

protected void attachBaseContext(Context newBase) {
context = G.changeAppLocale(G.appLang, newBase);
super.attachBaseContext(context);
}

再一次,字符串被正确加载,但样式没有(仅在 Android 8 上)。Android 不再支持不同的样式了吗?

最佳答案

我正在回答我自己的问题,以防有人遇到同样的问题并且可能对他们有所帮助。

我正在动态地扩充布局,并且我正在使用由旧上下文实例化的静态充气器。

// in class extended from Application

public static LayoutInflater globalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

当我充气时:

...
pageView = (ViewGroup) G.globalInflater.inflate(R.layout.layout_main_log, container, false);
...

但是当我更改应用程序语言环境时,充气器没有更新。因此, View 将从旧的语言环境中膨胀。现在,每次我想给 View 充气时,我都会实例化充气器:

 ...
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(inflater != null){
pageView = (ViewGroup) inflater.inflate(R.layout.layout_main_log, container, false);
}
...

谢谢大家的宝贵时间。

关于API 27 上的 Android 不同风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53120936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com