如何从内部更改Android应用程序的语言?

时间:2013-12-03 04:59:30

标签: android xml radio-button locale

我正在开发一个漫画应用程序。我有三个单选按钮,分别是英语,法语和西班牙语。当用户点击其中任何一个时,来自values-es,values-fr和values-en的strings.xml应该更改所需引用的字符串,但是我单击单选按钮它不会更改引用仍然在英语中,不要改为法语或西班牙语

2 个答案:

答案 0 :(得分:0)

我不确定这个答案会帮助你。但是,一小撮代码将以实际方式更改语言环境。试试这个,如果它工作正常,接受答案并+1:)

   Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale("Your language code(lower_case)");
    res.updateConfiguration(conf, dm);

答案 1 :(得分:0)

根据区域设置更改您的语言,如下所示:

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {

                if (pos == 1) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Es", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("ta");
                } else if (pos == 2) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Fr", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("hi");
                } else if (pos == 3) {

                    Toast.makeText(parent.getContext(),
                            "You have selected En", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("en");
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

        });
    }

    public void setLocale(String lang) {

        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}

之后创建您更改的值文件夹。在你的案例中,来自values-es,values-fr和values-en。

的strings.xml