Android應用語言切換功能

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

先看看demo的效果:

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;

@Override
protected 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

如果你喜歡我的文章就關注我的博客吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章