android中的语言没有改变

时间:2016-09-21 12:21:45

标签: android localization

我试图将语言更改为我的所有活动。但它没有改变。我最初尝试过在单个活动中更改语言。如何改变所有活动的语言。

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.language);

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        Button button=(Button)findViewById(R.id.button1) ;



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i= new Intent(LocalizationUpdaterActivity.this,HomePage.class);
                startActivity(i);
            }
        });
        spinner.setPrompt("select language");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,languages);

/*        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,
                android.R.layout.simple_spinner_item, languages);*/
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView arg0, View arg1,
                                       int arg2, long arg3) {
                Configuration config = new Configuration();
                switch (arg2) {

                    case 0:
                       Locale myLocale = new Locale("fa");
                        Resources res = getResources();
                        DisplayMetrics dm = res.getDisplayMetrics();
                        Configuration conf = res.getConfiguration();
                        conf.locale = myLocale;
                        res.updateConfiguration(conf, dm);
                        getBaseContext().getResources().updateConfiguration(
                                getBaseContext().getResources().getConfiguration(),
                                getBaseContext().getResources().getDisplayMetrics());
                        setContentView(R.layout.language);
                        Intent refresh = new Intent(LocalizationUpdaterActivity.this, LocalizationUpdaterActivity.class);
                        startActivity(refresh);
                        break;
                    default:
                        config.locale = Locale.TAIWAN;
                        break;
                }
                getResources().updateConfiguration(config, null);
            }

            public void onNothingSelected(AdapterView arg0) {
                // TODO Auto-generated method stub

            }
        });

    }


    private String[] languages = { "Inglese", "Italiano", "Francese" };

`

2 个答案:

答案 0 :(得分:0)

String language = "fr";
Locale locale = new Locale(language);
Locale.setDefault(locale);

来自SDK documentation

答案 1 :(得分:0)

你必须在发布时改变语言。

创建一个这样的类:

public class ApplicationBase extends Application
{

    public final String TAG = "ApplicationBase";

    @Override
    public void onCreate()
    {
        yourChangeLangMethod();
        super.onCreate();
    }
}

并在AndroidManifest.xml中插入:

<application
    android:name = ".ApplicationBase"
    android:icon = "@mipmap/ic_launcher"
    android:label = "@string/app_label"
    android:theme = "@style/AppTheme">