更改主题(暗模式)时采用默认字符串值,如何避免这种情况?

时间:2019-10-05 14:09:15

标签: android android-resources

我正在做一个项目,我想为用户提供“本地化”和“黑暗模式”。

为实现本地化->我在值-bn,值-es,值-hi,值-in等下为不同的语言环境定义了不同的字符串资源。

对于手动更改语言环境,我使用了此代码->

         Locale l = new Locale("our_locale_string");//It may be en,hi,..  etc.
        Locale.setDefault(l);
        Configuration config = new Configuration();
        config.locale = l;
        getResources().updateConfiguration(config,getResources().getDisplayMetrics()); 

我使用了两个styles.xml文件在主题之间进行切换

对于Light主题:

       <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="cardViewStyle">@style/CardView</item>
    <item name="android:windowBackground">@android:color/white</item>
      </style>  

对于深色主题:

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="cardViewStyle">@style/CardView</item>
    <item name="android:windowBackground">@android:color/black</item>
    </style>  

为了将主题更改为整个应用程序,我正在使用->

     if (mode) {   //mode is a boolean variable to toggle b/n themes
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }  

我可以在浅色主题中实现本地化,也可以将整个应用更改为暗色模式。问题来了,当我更改主题时,不使用它们各自的字符串,而是使用默认的strings.xml(通常为英语)。我该如何使用对应的语言环境strings.xml。

“项目视图”中的资源目录结构

res /值 res / values-hi res / values-zh   。   。   。 res / values-es 对于夜间模式,我有res / values-night / styles.xml

我还尝试过为value-xx文件夹(例如values-hi-night)中的上述每个不同字符串文件创建nigth模式资源文件,但这不起作用。

0 个答案:

没有答案