詳解Android更改APP語言模式的實(shí)現(xiàn)過程
一、效果圖

二、描述
更改Android項(xiàng)目中的語言,這個(gè)作用于只用于此APP,不會(huì)作用于整個(gè)系統(tǒng)
三、解決方案
(一)布局文件
<LinearLayout 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:orientation="vertical"
android:padding="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hellow" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeLanguage"
android:text="語言切換" />
</LinearLayout>
(二)MainActivity主頁面
package com.example.chinesepage;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 點(diǎn)擊按鈕,更換語言
*
* @param view
*/
public void changeLanguage(View view) {
Resources resources = getResources();
Configuration configuration = resources.getConfiguration(); // 獲取資源配置
if (configuration.locale.equals(Locale.CHINA)) { // 判斷當(dāng)前語言是否是中文
configuration.locale = Locale.ENGLISH; // 設(shè)置當(dāng)前語言配置為英文
} else {
configuration.locale = Locale.CHINA; // 設(shè)置當(dāng)前語言配置為中文
}
DisplayMetrics metrics = new DisplayMetrics();
resources.updateConfiguration(configuration, metrics); // 更新配置文件
sendBroadcast(new Intent("language")); // 發(fā)送廣播,廣播接受后重新開啟此Activtiy以重新初始化界面語言.
// Intent intent = new Intent(MainActivity.this, MainActivity.class); //或者可以直接跳轉(zhuǎn)MainActivity
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //去除掉跳轉(zhuǎn)的動(dòng)畫,讓用戶看起來好像沒有跳轉(zhuǎn)的感覺
// startActivity(intent);
finish();
}
}
(三)ChangeReceiver廣播類
package com.example.chinesepage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 自定義廣播類 語言改變后重啟Activity
*
* @author asus
*
*/
public class ChangeReceiver extends BroadcastReceiver {
private Intent mIntent;
@Override
public void onReceive(Context context, Intent intent) {
mIntent = new Intent(context, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
(四)在Res下創(chuàng)建Values-en文件夾,復(fù)制String.xml,并且把里面的中文改成英文,實(shí)現(xiàn)國際化.

values/strings.xml
<resources> <string name="app_name">語言切換</string> <string name="hello_world">你好,World!</string> <string name="action_settings">設(shè)置</string> <string name="hellow">你好</string> </resources>
values-en/strings.xml
<resources> <string name="app_name">ChinesePage</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="hellow">Hellow</string> </resources>
(五)注冊廣播(這個(gè)別忘了~)
<receiver android:name="com.example.chinesepage.ChangeReceiver" >
<intent-filter>
<action android:name="language" />
</intent-filter>
</receiver>
總結(jié)
以上就是詳解Android更改APP語言模式的實(shí)現(xiàn)過程的全部內(nèi)容,希望對大家開發(fā)Android有所幫助,如果有疑問歡迎留言討論。
- Android實(shí)現(xiàn)app應(yīng)用多語言切換功能
- Android app應(yīng)用多語言切換功能實(shí)現(xiàn)
- android動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語言的方法
- android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)
- Android 7.0以上版本實(shí)現(xiàn)應(yīng)用內(nèi)語言切換的方法
- Android 系統(tǒng)語言切換監(jiān)聽和設(shè)置實(shí)例代碼
- Android實(shí)現(xiàn)應(yīng)用內(nèi)置語言切換功能
- Android實(shí)現(xiàn)系統(tǒng)語言切換功能
- Android App中進(jìn)行語言的切換
相關(guān)文章
ubuntu 12.10 上 android 編譯環(huán)境搭建的深入解析
本篇文章是對ubuntu 12.10上android 編譯環(huán)境的搭建進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Google 開發(fā)Android MVP架構(gòu)Demo深入解析
這篇文章主要為大家介紹了Google 開發(fā)Android MVP架構(gòu)Demo深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android Gradle Build Error:Some file crunching failed, see l
這篇文章主要介紹了Android Gradle Build Error:Some file crunching failed, see logs for details解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android編程實(shí)現(xiàn)動(dòng)畫自動(dòng)播放功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)動(dòng)畫自動(dòng)播放功能,結(jié)合實(shí)例形式分析了Android動(dòng)畫自動(dòng)播放功能的實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Android SurfaceView預(yù)覽變形完美解決方法
本篇文章主要介紹了Android SurfaceView預(yù)覽變形完美解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android高級UI特效仿直播點(diǎn)贊動(dòng)畫效果
這篇文章主要介紹了Android高級UI特效仿直播點(diǎn)贊動(dòng)畫效果,最近比較火的抖音快手直播視頻都有這樣的效果,下面腳本之家小編給大家?guī)韆ndroid 仿直播點(diǎn)贊效果的實(shí)現(xiàn)代碼,需要的朋友參考下吧2018-03-03

