在android中按菜单时更改应用程序的语言

时间:2013-12-06 12:14:16

标签: android

我想在按下菜单按钮时更改我的android的语言,但它不起作用,这是我的代码:

@Override
        public boolean onContextItemSelected(MenuItem item) {

          //  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            switch(item.getItemId()){

                case R.id.english:
                    Locale locale1 = new Locale("en");//fr is the code for french language
                    Locale.setDefault(locale1);
                    Configuration config1 = new Configuration();
                    config1.locale = locale1;
                    getBaseContext().getResources().updateConfiguration(config1,
                          getBaseContext().getResources().getDisplayMetrics());
                    clicked=false;
                    onCreate(new Bundle());
                    break;
                case R.id.french:
                    Locale locale2 = new Locale("fr");//fr is the code for french language
                    Locale.setDefault(locale2);
                    Configuration config2 = new Configuration();
                    config2.locale = locale2;
                    getBaseContext().getResources().updateConfiguration(config2,
                          getBaseContext().getResources().getDisplayMetrics());
                    clicked=false;
                    onCreate(new Bundle());

                    break;

            }
            return true;

        }

我在menu.xml文件中添加了菜单项,但我不知道它为什么不起作用

1 个答案:

答案 0 :(得分:1)

我解决了它,只需使用

onOptionsItemSelected(MenuItem item)

而不是:

onContextItemSelected(MenuItem item)