Android應用語言切換功能

來源:互聯網
上載者:User

標籤:android   中英文切換   

因為公司的產品大部分是銷售到國外的,所以領導要求app有語言切換功能。我在網上搜了些相關知識並實現了功能,在這裡做個中英文切換的demo記錄下來。

先看看demo的效果:

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/84/4F/wKiom1eMiBzhgdUqAB0BiAv51Ng877.gif" title="demo.gif" alt="wKiom1eMiBzhgdUqAB0BiAv51Ng877.gif" />

效果就是這樣子。當然也可以做成其他語言的切換,具體根據需求而定。


原理其實很簡單,就是多個strings.xml進行切換然後在重新整理Activity。

首先,在AndroidManifest.xml檔案中的每個需要切換語言的Activity中加入android:configChanges="locale"。


然後在res檔案夾下添加對應語種的values檔案:

比如中文簡體就是values-zh-rCN、英文就是values-en。

當然還有其他語種的,這裡我只做了兩個常用的有需要的可以去查看各國語言縮寫、各國語言簡稱。其原理都是一樣的。


values-zh-rCN/strings.xml:

<resources>    <string name="app_name">LanguageDemo</string>    <string name="chinese">中文</string>    <string name="english">英文</string>    <string name="red">紅色</string>    <string name="orange">黃色</string>    <string name="blue">藍色</string>    <string name="green">綠色</string>    <string name="purple">紫色</string></resources>


values-en/strings.xml:

<resources>    <string name="app_name">LanguageDemo</string>    <string name="chinese">Chinese</string>    <string name="english">English</string>    <string name="red">Red</string>    <string name="orange">Orange</string>    <string name="blue">Blue</string>    <string name="green">Green</string>    <string name="purple">Purple</string></resources>


布局檔案:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.lg.languagedemo.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="Chinese"        android:text="@string/chinese" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:onClick="English"        android:text="@string/english" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_centerInParent="true"        android:orientation="vertical">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/red"            android:textColor="@android:color/holo_red_dark"            android:textSize="16sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="@string/orange"            android:textColor="@android:color/holo_orange_dark"            android:textSize="16sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="@string/blue"            android:textColor="@android:color/holo_blue_dark"            android:textSize="16sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="@string/green"            android:textColor="@android:color/holo_green_dark"            android:textSize="16sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="@string/purple"            android:textColor="@android:color/holo_purple"            android:textSize="16sp" />    </LinearLayout></RelativeLayout>


最後是切換語言的核心代碼:

private Configuration configuration;private DisplayMetrics displayMetrics;private Resources resources;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    resources = getResources();// 獲得res資來源物件    configuration = resources.getConfiguration();// 獲得設定對象    displayMetrics = resources.getDisplayMetrics();}//中文public void Chinese(View view) {    configuration.locale = Locale.SIMPLIFIED_CHINESE;    resources.updateConfiguration(configuration, displayMetrics);    startActivity(new Intent(MainActivity.this,MainActivity.class));    finish();}//英文public void English(View view) {    configuration.locale = Locale.US;    resources.updateConfiguration(configuration, displayMetrics);    startActivity(new Intent(MainActivity.this,MainActivity.class));    finish();}

當然重新整理頁面也不止這一種方法可以用onCreate(),不過這個方法限制比較多。還有recreate(),使用這個方法螢幕會閃一下。


那麼源碼地址:http://down.51cto.com/data/2229088

如果你喜歡我的文章就關注我的部落格吧!

本文出自 “Android開發專欄” 部落格,請務必保留此出處http://liuyvhao.blog.51cto.com/11690759/1827424

Android應用語言切換功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.