App內(nèi)切換語言詳解
前幾天客戶提需求,對(duì)App增加一個(gè)功能,這個(gè)功能目前市面上已經(jīng)很常見,那就是應(yīng)用內(nèi)切換語言。啥意思,就是 英、中、法、德、日。。。語言隨意切換。
(本案例采用Data-Bingding模式,麻麻再也不用擔(dān)心我findViewBy不到Id了哈哈,開個(gè)玩笑)
先上示例圖:
代碼實(shí)現(xiàn):
布局文件(Data-Binding模式),很簡(jiǎn)單就是兩行文字
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tnnowu.android.switchlanguage.MainActivity"> <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/title" android:textSize="30sp" android:textStyle="bold" /> <TextView android:id="@+id/descTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/titleTextView" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/desc" android:textSize="20sp" /> </RelativeLayout> </layout>
從實(shí)例中我們可以看到右上角是有Menu
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/language_english" android:orderInCategory="100" android:title="@string/menu_english" /> <item android:id="@+id/language_simplified_chinese" android:orderInCategory="100" android:title="@string/menu_simplified_chinese" /> <item android:id="@+id/language_turkish" android:orderInCategory="100" android:title="@string/menu_turkish" /> <item android:id="@+id/language_japanese" android:orderInCategory="100" android:title="@string/menu_japanese" /> </menu>
(既然是多語言,所以就要有N個(gè)strings)
本案例我創(chuàng)建了4種語言。
好的,Menu的布局寫完了,接下來就是實(shí)現(xiàn)Menu功能,記住實(shí)現(xiàn)Menu就兩套代碼,一個(gè) onCreateOptionsMenu , 另一個(gè)是 onOptionsItemSelected 。
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.language_english) { updateViews("en"); } else if (id == R.id.language_simplified_chinese) { updateViews("zh"); } else if (id == R.id.language_turkish) { updateViews("tr"); } else if (id == R.id.language_japanese) { updateViews("ja"); } return super.onOptionsItemSelected(item); }
在這里,可以看到,我們自定義一個(gè) updateViews() 方法,用來實(shí)現(xiàn)切換預(yù)言時(shí)界面的改變
private void updateViews(String languageCode) { Context context = LocaleHelper.setLocale(this, languageCode); Resources resources = context.getResources(); mBinding.titleTextView.setText(resources.getString(R.string.title)); mBinding.descTextView.setText(resources.getString(R.string.desc)); setTitle(resources.getString(R.string.toolbar_title)); }
公布一個(gè) 語言判斷的類 LocaleHelper
public class LocaleHelper { private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"; public static Context onAttach(Context context) { String lang = getPersistedData(context, Locale.getDefault().getLanguage()); return setLocale(context, lang); } public static Context onAttach(Context context, String defaultLanguage) { String lang = getPersistedData(context, defaultLanguage); return setLocale(context, lang); } public static String getLanguage(Context context) { return getPersistedData(context, Locale.getDefault().getLanguage()); } public static Context setLocale(Context context, String language) { persist(context, language); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return updateResources(context, language); } return updateResourcesLegacy(context, language); } private static String getPersistedData(Context context, String defaultLanguage) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); } private static void persist(Context context, String language) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); editor.putString(SELECTED_LANGUAGE, language); editor.apply(); } @TargetApi(Build.VERSION_CODES.N) private static Context updateResources(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Configuration configuration = context.getResources().getConfiguration(); configuration.setLocale(locale); return context.createConfigurationContext(configuration); } @SuppressWarnings("deprecation") private static Context updateResourcesLegacy(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); configuration.locale = locale; resources.updateConfiguration(configuration, resources.getDisplayMetrics()); return context; } }
最后還要做的操作就是,自定義一個(gè)Application類,用來設(shè)定App的默認(rèn)語言(當(dāng)然了,要將這個(gè)Application應(yīng)用到Manifest中)
public class BaseApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(LocaleHelper.onAttach(base, "en")); } }
本案例實(shí)現(xiàn)App內(nèi)語言切換代碼量不大,通俗易懂,無垃圾代碼。
示例代碼下載地址:App內(nèi)切換語言
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android自定義View實(shí)現(xiàn)支付寶咻一咻效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)支付寶咻一咻效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android開發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果
這篇文章主要介紹了Android開發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果的方法,文章最后還附帶了監(jiān)聽程序是否進(jìn)入后臺(tái)的判斷方法,需要的朋友可以參考下2016-02-02android中Activity詳解(生命周期、以各種方式啟動(dòng)Activity、狀態(tài)保存,完全退出等)
本篇文章詳細(xì)的介紹了Activity,包括生命周期、以各種方式啟動(dòng)Activity、狀態(tài)保存,完全退出等,有興趣的可以了解一下。2016-11-11Android通過json向MySQL中讀寫數(shù)據(jù)的方法詳解【寫入篇】
這篇文章主要介紹了Android通過json向MySQL中讀寫數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android json類的定義、調(diào)用及php接收json數(shù)據(jù)并寫入mysql的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動(dòng)畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05Android ReboundScrollView仿IOS拖拽回彈效果
這篇文章主要為大家詳細(xì)介紹了Android ReboundScrollView仿IOS拖拽回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android使用CardView實(shí)現(xiàn)圓角對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android使用CardView實(shí)現(xiàn)圓角對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11