android 多語言處理


1) 代碼中獲取系統語言:

Locale.getDefault().getLanguage()  //語言:取到的比如中文為zh,英文為en,日文為ko;
Locale.getDefault().toString()//具體的類別:比如繁體為zh_TW,簡體為zh_CN。英文中有en_GB;日文有ko_KR。

 

2)Xml中使用多語言:

第一步:將應用中的所有需要顯示的字符串,如菜單,標題、文本,以及提示信息等都定義到values/strings.xml之中,注意name屬性不能有重復

第二步:將應用中的字符串替換成從strings.xml中引用的形式,如 getResources().getString(R.string.appname)

第三步:真正的多語言設置了,需要新建一個xml文件(Eclipse的 File - New - Android xml file),會顯示如圖畫面!

 

 

AndroidManifest.xml中:

每一個Activity中都要加: android:configChanges="locale"。

manifest節點中插入節點:
<supports-screens android:smallScreens="true"
	android:normalScreens="true" android:largeScreens="true"
	android:anyDensity="true" />

 

3)代碼中指定運行語言

public void chooseLanguage(Locale locale) {

	Resources resources = getResources();//獲得res資源對象

	Configuration config = resources.getConfiguration();//獲得設置對象

	DisplayMetrics dm = resources .getDisplayMetrics();//獲得屏幕參數:主要是分辨率,像素等。

	config.locale = locale; //語言

	 resources.updateConfiguration(config, dm);

}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM