參考源碼:http://download.csdn.net/detail/barryhappy/7176709html
將使用support.v7的項目修改了一下,變成了一個不使用actionbaractivity而是activity的。減小報錯。java
http://download.csdn.net/detail/mcdullsin/8290923android
1、在說多語言的時候首先說說如何減小「hard code」服務器
什麼是「hard code」,例如:java文件中直接使用了漢字字符串等。正確作法是將它放在資源文件中,並在須要的地方進行引用。app
說一說如何在java文件等地方使用string.xml中的資源:ide
獲取string.xml文件裏面的值有幾個不一樣的地方。佈局
1.在AndroidManifest.xml與layout等xml文件裏:ui
android:text="@string/resource_name" this
2.在activity裏:spa
方法一:this.getString(R.string.resource_name);
方法二:getResources().getString(R.string.resource_name);
3.在其餘java文件(必須有Context或pplication)
方法一: context.getString(R.string.resource_name);
方法二: application.getString(R.string.resource_name);
2、接下來分析多語言的源碼及如何使用
首先效果截圖:
這位大哥的博客中很好地說明了android多語言國際化:
http://www.cnblogs.com/bluestorm/archive/2013/04/01/2993554.html
android的多語言原理是這樣:
它會檢查android手機的默認配置,而後到具體的values下面去讀取字符串。
那位大哥博文中的話:
「
在Android工程的res目錄下,經過定義特殊的文件夾名稱就能夠實現多語言支持。好比咱們的程序兼容簡體中文、英文,在values文件夾中創建默認strings.xml,再創建values-zh-rCN文件夾。
在每一個文件夾裏放置一個strings.xml,strings.xml裏是各類語言字符串。若是涉及到參數配置類xml文件夾名稱也要改爲xml-zh、xml。這樣在android的系統中進行語言切換,所開發的程序也會跟着切換語言。
」
特殊的文件夾名!
說明文件夾名每一個是固定的。
那麼分析這篇博文中的項目作了什麼事情。
它作的事點擊menu會彈出語言設置,而後再進行語言的切換。
Java源碼以下:
package barry.demo.multilanguage; import java.util.Locale; import android.support.v4.app.Fragment; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Configuration config = getResources().getConfiguration(); switch (item.getItemId()) { case R.id.action_english: config.locale = Locale.ENGLISH; break; case R.id.action_simple_chinses: config.locale = Locale.SIMPLIFIED_CHINESE; break; case R.id.action_traditional_chinese: config.locale = Locale.TRADITIONAL_CHINESE; break; default: return true; } getResources().updateConfiguration(config, getResources().getDisplayMetrics()); ((TextView)findViewById(R.id.textViewHello)).setText(R.string.hello_world);; return true; } }
主佈局文件以下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="barry.demo.multilanguage.MainActivity" tools:ignore="MergeRootFrame" > <TextView android:id="@+id/textViewHello" android:layout_width="match_parent" android:layout_height="218dp" android:gravity="center" android:text="@string/hello_world" /> </FrameLayout>
菜單佈局文件以下:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="barry.demo.multilanguage.MainActivity" > <item android:id="@+id/action_english" android:orderInCategory="100" android:title="@string/english"/> <item android:id="@+id/action_simple_chinses" android:orderInCategory="100" android:title="@string/simple_chinese"/> <item android:id="@+id/action_traditional_chinese" android:orderInCategory="100" android:title="@string/traditional_chinese"/> </menu>
在這裏關鍵有一個問題就是:
當點擊按鈕以後,會對文件進行刷新,是每一個字符串textview、listview都要刷新。
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
((TextView)findViewById(R.id.textViewHello)).setText(R.string.hello_world);;
若是涉及到服務器就很差刷新了。
並且即便本地文件刷新量也有點大了。
因此作法是不能在每一個頁面都有語言切換按鈕,而應該將語言切換按鈕放在登錄頁。