关于Androd Locale改变,应用本身资源locale问题

来源:互联网 发布:python字符串能更改吗 编辑:程序博客网 时间:2024/06/08 09:48

After a good night of spleep, I found the answer on the Web (a simple Google search on the following line "getBaseContext().getResources().updateConfiguration(mConfig, getBaseContext().getResources().getDisplayMetrics());"), here it is :

link text=> this link also showsscreenshots of what is happening !

Density was the issue here, I needed to have this in the AndroidManifest.xml

<supports-screensandroid:smallScreens="true"android:normalScreens="true"android:largeScreens="true"android:anyDensity="true"/>

The most important is the android:anyDensity =" true ".

Don't forget to add the following in the manifest (for every activity) :

android:configChanges="locale"
link|improve this answer


Through the original question is not exactly about the locale itself all other locale related questions are referencing to this one. That's why I wanted to clarify the issue here. I used this question as a starting point for my own locale switching code and found out that the method is not exactly correct. It works, but only until any configuration change (e.g. screen rotation) and only in that particular Activity. Playing with a code for a while I have ended up with the following approach:

I have extended android.app.Application and added the following code:

public class MyApplication extends Application{    private Locale locale = null;    @Override    public void onConfigurationChanged(Configuration newConfig)    {        super.onConfigurationChanged(newConfig);        if (locale != null)        {            newConfig.locale = locale;            Locale.setDefault(locale);            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());        }    }    @Override    public void onCreate()    {        super.onCreate();        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);        Configuration config = getBaseContext().getResources().getConfiguration();        String lang = settings.getString(getString(R.string.pref_locale), "");        if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))        {            locale = new Locale(lang);            Locale.setDefault(locale);            config.locale = locale;            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());        }    }}

This code ensures that every Activity will have custom locale set and it will not be reset on rotation and other events.

I have also spent a lot of time trying to make the preference change to be applied immediately but didn't succeed: the language changed correctly on Activity restart, but number formats and other locale properties were not applied until full application restart.

link|improve this answer


原创粉丝点击