【问题标题】:Android - Forced locale reset on orientation changesAndroid - 方向更改时强制重置区域设置
【发布时间】:2010-02-24 07:52:34
【问题描述】:

我尝试将应用程序中的区域设置强制为用户指定的区域。由于这可能用于演示,我想更改应用程序中的语言/区域设置,而不是随时更改整个设备。

我环顾四周并尝试使用我在这里找到的每一个提示。结果:我可以使用新语言重新开始我的测试活动,但如果我改变方向,区域设置将始终重置为设备一。

uploaded a minimalistic project,所以你可以重现我的问题。请忽略 UI 的缩小,这并不重要 :)

【问题讨论】:

    标签: android locale orientation


    【解决方案1】:

    此代码 sn-p 可以满足您的需求,我已经对其进行了测试。假设您希望用户能够通过主活动中的菜单切换语言。您可以通过将语言标志保存为用户首选项并在“onCreate()”和“onConfigurationChanged()”上进行检查,如下所示:

        public class MainActivity extends Activity {
    
    SharedPreferences mPrefs1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPrefs1 = PreferenceManager.getDefaultSharedPreferences(this);
        String languageToLoad = mPrefs1.getString("languagePref", Locale.getDefault().getLanguage());
        Locale locale = new Locale(languageToLoad); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
    
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
        mPrefs1 = PreferenceManager.getDefaultSharedPreferences(this);
        String languageToLoad = mPrefs1.getString("languagePref", Locale.getDefault().getLanguage());
        Locale locale = new Locale(languageToLoad); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
        setContentView(R.layout.activity_main);
    }
    
    // Declaring the Menu options
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {       
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    
    //handle menu item selection
    public boolean onOptionsItemSelected(MenuItem item)
    {
      switch (item.getItemId()) {
        case R.id.menu_switch_language:
            String languageToLoad  = getResources().getString(R.string.switch_language);
    
            mPrefs1 = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = mPrefs1.edit();
            editor.putString("languagePref", languageToLoad);
            editor.commit(); // Very important to save the preference                      
            finish();
            startActivity(getIntent());
            return true;
        default:
            return super.onOptionsItemSelected(item);
      }
    }   
    

    }

    【讨论】:

    • Resources#updateConfiguration 已弃用,请使用 Context#createConfigurationContext(Configuration)
    【解决方案2】:

    我刚刚看到你的代码。

    检查您使用的Locale 来自 java.util 而不是来自 android。您的代码不会更改手机的区域设置。

    测试它:

    • 我将手机设置为西班牙语。
    • 打开您的应用
    • 点击按钮
    • 回家
    • 图标是西班牙语而不是英语。

    如果您只是将区域设置设置为默认值,并且在屏幕旋转后能够看到相同的值:

    代替:

    android:configChanges="locale"
    

    使用:

    android:configChanges="locale|orientation"
    

    【讨论】:

      【解决方案3】:

      对我有用的是在扩展布局之前设置语言环境:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          String userLocale = "Wolof"; // adapt to your need
          Locale locale = new Locale(userLocale);
          Locale.setDefault(locale);
          Configuration config = new Configuration();
          config.locale = locale;
          getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
      
          setContentView(R.layout.my_activity);
      
          ...
      
      }
      

      【讨论】:

        【解决方案4】:

        是否因为您的 Activity 在方向更改时重新启动而发生区域设置重置?

        如果是这样,您可以拦截或阻止方向更改Activity restart on rotation Android

        【讨论】:

        • 我使用 finish() 和 startActivity() “重新启动”我的 Activity 以强制重新加载使用新语言环境的 UI。方向更改总是重置我的语言环境
        【解决方案5】:

        您应该在 Application::onCreateApplication::onConfigurationChanged 中强制使用您的语言环境。 不要忘记在 AndroidManifest.xml

        中注册您的应用程序类

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-26
          • 2013-04-24
          • 1970-01-01
          • 2018-12-29
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多