为应用程序定义了不同的样式和字符串。在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;
    }

我把它应用到活动中,就像这样:
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);
...

但当我更改应用程序的区域设置时,这个充气器没有更新。因此,视图将从旧的区域设置中膨胀。现在,每当我想为视图充气时,我都要实例化充气器:
 ...
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);
}
    ...

谢谢大家抽出时间。

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

10-12 00:33
查看更多