在应用程序内更改区域设置

Hub*_*ert 195 android locale menu

我的用户可以更改应用内的区域设置(他们可能希望将手机设置保留为英文,但可以用法语,荷兰语或任何其他语言阅读我的应用内容...)

为什么这个在1.5/1.6中完全正常但在2.0中不再是???

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case 201:
        Locale locale2 = new Locale("fr"); 
        Locale.setDefault(locale2);
        Configuration config2 = new Configuration();
        config2.locale = locale2;
        getBaseContext().getResources().updateConfiguration(
            config2, getBaseContext().getResources().getDisplayMetrics());
        // loading data ...
        refresh();
        // refresh the tabs and their content
        refresh_Tab ();   
     break;
     case 201: etc...
Run Code Online (Sandbox Code Playgroud)

问题是,每当用户通过以上代码行时,MENU"缩小"越来越多......

这是缩小的菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 100, 1, "REFRESH").setIcon(android.R.drawable.ic_menu_compass);
    SubMenu langMenu = menu.addSubMenu(0, 200, 2, "NL-FR").setIcon(android.R.drawable.ic_menu_rotate);
        langMenu.add(1, 201, 0, "Nederlands");
        langMenu.add(1, 202, 0, "Français");
    menu.add(0, 250, 4, R.string.OptionMenu2).setIcon(android.R.drawable.ic_menu_send);
    menu.add(0, 300, 5, R.string.OptionMenu3).setIcon(android.R.drawable.ic_menu_preferences);
    menu.add(0, 350, 3, R.string.OptionMenu4).setIcon(android.R.drawable.ic_menu_more);
    menu.add(0, 400, 6, "Exit").setIcon(android.R.drawable.ic_menu_delete);

    return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)

我应该在API Level 5中做些什么来重新开始这项工作?

如果你想测试,这里是完整的代码:

import java.util.Locale;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class Main extends Activity {
    /** Called when the activity is first created. */


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        SubMenu langMenu = menu.addSubMenu(0, 200, 2, "NL-FR").setIcon(android.R.drawable.ic_menu_rotate);
            langMenu.add(1, 201, 0, "Nederlands");
            langMenu.add(1, 202, 0, "Français");

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){

        case 201:

            Locale locale = new Locale("nl"); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            Toast.makeText(this, "Locale in Nederlands !", Toast.LENGTH_LONG).show();
            break;

        case 202:

            Locale locale2 = new Locale("fr"); 
            Locale.setDefault(locale2);
            Configuration config2 = new Configuration();
            config2.locale = locale2;
            getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());

            Toast.makeText(this, "Locale en Français !", Toast.LENGTH_LONG).show();
            break;  

        }
        return super.onOptionsItemSelected(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是清楚的:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.cousinHub.ChangeLocale"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Main"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        <uses-sdk android:minSdkVersion="3" /> 
    </manifest>
Run Code Online (Sandbox Code Playgroud)

这就是我发现的:

<uses-sdk android:minSdkVersion="5" />
Run Code Online (Sandbox Code Playgroud)

=>它的工作很精细......

<uses-sdk android:minSdkVersion="3" />
Run Code Online (Sandbox Code Playgroud)

=>每次更改语言环境时菜单都会缩小!

因为我希望1.5版本的用户可以访问我的应用程序,我该怎么办?

And*_*kov 148

通过原始问题并不完全是关于语言环境本身所有其他与语言环境相关的问题都引用了这个问题.这就是我想在这里澄清问题的原因.我使用这个问题作为我自己的语言环境切换代码的​​起点,发现该方法并不完全正确.它可以工作,但只能在任何配置更改(例如屏幕旋转)之前,并且仅在该特定活动中.玩了一段时间的代码我最终采用了以下方法:

我扩展了android.app.Application并添加了以下代码:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码确保每个Activity都具有自定义区域设置,并且不会在轮换和其他事件上重置.

我还花了很多时间尝试使首选项更改立即应用但未成功:语言在Activity重新启动时正确更改,但在完全重新启动应用程序之前,数字格式和其他语言环境属性未应用.

改变为 AndroidManifest.xml

不要忘记添加android:configChanges="layoutDirection|locale"为每一个活动在AndroidManifest,以及android:name=".MyApplication"<application>元素.

  • 请注意,您必须**永远不要**修改Android为您提供的配置对象(`config`和`newConfig`).你应该使用`config = new Configuration(config);`来制作副本.我只花了一个小时调试这段代码,然后才发现问题(这导致活动无休止地闪烁). (17认同)

Hub*_*ert 61

经过一夜安眠之后,我在网上找到了答案(以下一行简单的谷歌搜索" getBaseContext().getResources().updateConfiguration(mConfig, getBaseContext().getResources().getDisplayMetrics());"),这里是:

link text =>此链接还显示screenshots正在发生的事情!

密度是这里的问题,我需要在AndroidManifest.xml中有这个

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
/>
Run Code Online (Sandbox Code Playgroud)

最重要的是android:anyDensity ="true".

不要忘记在AndroidManifest.xml每个活动中添加以下内容(适用于Android 4.1及更低版本):

android:configChanges="locale"
Run Code Online (Sandbox Code Playgroud)

在此处为Android 4.2(API级别17)解释构建时需要此版本:

android:configChanges="locale|layoutDirection"
Run Code Online (Sandbox Code Playgroud)

  • 由于谷歌(传统应用策略 - 第9点:http://developer.android.com/intl/fr/guide/practices/screens_support.html#strategies)的建议,我有了android:anyDensity ="false".那么小心! (3认同)
  • 更改区域设置后,按钮上的文本不会更改.我想知道是否有任何解决方案来刷新包含字符串资源的所有小部件.现在我在OnRestart()中使用setText(getString(R.string.button_label))重绘文本.(当然,重启应用程序后会发生变化.) (2认同)

Rob*_* B. 56

在Android M中,顶级解决方案无效.我已经写了一个帮助类来修复你应该从你的Application类和所有的Activity调用它(我建议创建一个BaseActivity,然后让所有的Activity继承它.

注意:这也将支持正确的RTL布局方向.

助手班:

public class LocaleUtils {

    private static Locale sLocale;

    public static void setLocale(Locale locale) {
        sLocale = locale;
        if(sLocale != null) {
            Locale.setDefault(sLocale);
        }
    }

    public static void updateConfig(ContextThemeWrapper wrapper) {
        if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Configuration configuration = new Configuration();
            configuration.setLocale(sLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

    public static void updateConfig(Application app, Configuration configuration) {
        if (sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            // We must use the now-deprecated config.locale and res.updateConfiguration here,
            // because the replacements aren't available till API level 24 and 17 respectively.
            config.locale = sLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用:

public class App extends Application {
    public void onCreate(){
        super.onCreate();

        LocaleUtils.setLocale(new Locale("iw"));
        LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleUtils.updateConfig(this, newConfig);
    }
}
Run Code Online (Sandbox Code Playgroud)

BaseActivity:

public class BaseActivity extends Activity {
    public BaseActivity() {
        LocaleUtils.updateConfig(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是接受的答案.确保updateConfig应该从构造函数调用,而不是从活动类的onCreate调用,我正在做这个错误.. (10认同)
  • 这取决于您如何在程序中实现这一点。如果你覆盖了 onConfigurationChanged() 你不需要调用 .updateConfig() 这就是你收到错误的原因,因为它被调用了两次。如果您说仅调用 .setLocale() 不起作用,这是因为您需要通过在活动上调用 recreate() 或在活动/片段中调用您自己的方法来刷新您的用户界面(我确实更喜欢前者,即使它并不顺利,但你永远不会担心问题) (2认同)
  • // import android.support.v7.view.ContextThemeWrapper; import android.view.ContextThemeWrapper; //更正名称空间 (2认同)

trg*_*lia 10

这是我对Andrey的答案的评论,但我不能在评论中包含代码.

您是否从您的主要活动中调用了偏好活动?你可以把它放在简历上......

@Override
protected void onResume() {
    if (!(PreferenceManager.getDefaultSharedPreferences(
            getApplicationContext()).getString("listLanguage", "en")
            .equals(langPreference))) {
        refresh();
    }
    super.onResume();
}

private void refresh() {
    finish();
    Intent myIntent = new Intent(Main.this, Main.class);
    startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)

  • 你的意思是,如果我想在运行时更改语言,那么我必须完成该活动并再次启动该活动以获得更改?有没有其他方法可以让它改变,因为每次完成活动并重新开始它并不好. (2认同)

nik*_*3ro 6

我无法使用android:anyDensity ="true",因为我的游戏中的对象将完全不同......似乎这也有诀窍:

// creating locale
Locale locale2 = new Locale(loc); 
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;

// updating locale
mContext.getResources().updateConfiguration(config2, null);