Android Local语言设置

来源:互联网 发布:人工智能电影我看哭了 编辑:程序博客网 时间:2024/06/03 17:03

Android Local语言设置

 (2013-11-21 16:07:24)

标签: 

android

 

语言

 

local

 

configuration对象

分类: ANDROID
  在Android中每个应用程序都维护着一个自身的配置。被封装在 android.content.res.Configuration 类里,通过该类,我们可以修改语言参数,实现对应用语言的变更。
  本例利用Configuration 实现应用程序语言设置:
1 首先获取Configuration 类:
  Resources resource getResources(); 
  Configuration config resource.getConfiguration();  
2 设置config的local属性
  locale属性为一个Locale类的对象,   
  Locale locale = getResources().getConfiguration().locale;//获得local对象
  String country = locale.getCountry(); //可以通过local获得相关的属性  如国家
3 设置config属性
  //设置应用的语言为英语
  config.locale Locale.ENGLISH;
 //设置成系统默认的语言:
  config.locale Locale.getDefault();
4 设置好config属性后,需要更新
  getBaseContext().getResources().updateConfiguration(config, null);  

  语言设置完成后,应用并不会自动刷新当前已经打开的Activity,所以为了刷新整个应用,需要重新启动一下应用:
Intent intent new Intent();   
intent.setClass(thisMainActivity.class);   
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
this.startActivity(intent);  
-------------------------------------------------------------------------------
 另外需要注意的是,需要在Manifest.xml文件中的Activity中配置android:configChanges属性,通过配置该属性,Activity可以捕捉设备状态的变化
  • <</span>activity  
  •             android:name=".MainActivity"  
  •             android:label="@string/title_activity_main"  
  •             android:configChanges="locale" >  //捕捉local的变化
  •             <</span>intent-filter>  
  •                 <</span>action android:name="android.intent.action.MAIN" />  
  •                 <</span>category android:name="android.intent.category.LAUNCHER" />  
  •             </</span>intent-filter>  
  • </</span>activity>  

转自:http://blog.sina.com.cn/s/blog_6b936f150101gxnv.html
0 0
原创粉丝点击