设置本地化编程不工作? [英] Setting Locale Programmatically not working?

查看:142
本文介绍了设置本地化编程不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的编程设置区域设置为德,并没有按预期并显示默认语言(英文)是手工设置的活动。请帮忙

 公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        //编程设置区域和语言
        区域设置区域设置=新的语言环境(德);
        Locale.setDefault(区域);
        配置配置=新配置();
        config.locale =区域;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();

        的setContentView(R.layout.main);
意向意图=新的意图(LatestLocalizationActivity.this,AnotherActivity.class);
       startActivity(意向);
}
 

解决方案

您已经在水库 - >价值-DE文件夹中添加的strings.xml文件?

 公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        //编程设置区域和语言
                    的setContentView(R.layout.main);
                    配置= getBaseContext()getResources()getConfiguration()。
                    区域=新的语言环境(德);
                    Locale.setDefault(区域);
                    config.locale =区域;
                    。getBaseContext()getResources()updateConfiguration(。配置,getBaseContext()getResources()getDisplayMetrics());
                    刷新();



       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();


}



@覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        。组态配置= getBaseContext()getResources()getConfiguration();
      //这里刷新你的意见
        Locale.setDefault(区域);
        config.locale =区域;
      super.onConfigurationChanged(NEWCONFIG);
    }



私人无效刷新(){
        完();
        意图myIntent =新的意图(yourActivity.this,yourActivity.class);
        startActivity(myIntent);
    }
 

I have an activity where I programmatically set the locale to "de" and it doesn't work as expected and displays the default language (English text) that is manually set. Please help

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //Programmatically sets the locale and language  
        Locale locale = new Locale("de");  
        Locale.setDefault(locale);  
        Configuration config = new Configuration();  
        config.locale = locale;   
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());   

       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();  

        setContentView(R.layout.main);  
Intent intent=new Intent(LatestLocalizationActivity.this,AnotherActivity.class);  
       startActivity(intent);  
}

解决方案

had you add the Strings.xml file in res->value-de folder?

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //Programmatically sets the locale and language
                    setContentView(R.layout.main); 
                    config = getBaseContext().getResources().getConfiguration(); 
                    locale = new Locale("de");
                    Locale.setDefault(locale);
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                    refresh();



       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();  


}



@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Configuration config = getBaseContext().getResources().getConfiguration();
      // refresh your views here
        Locale.setDefault(locale);
        config.locale = locale;
      super.onConfigurationChanged(newConfig);
    }



private void refresh() {
        finish();
        Intent myIntent = new Intent(yourActivity.this, yourActivity.class);
        startActivity(myIntent);
    }

这篇关于设置本地化编程不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆