這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android開發(fā)中怎么在App中實現(xiàn)一個內(nèi)語言切換功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都做網(wǎng)站、網(wǎng)站設(shè)計介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)建站擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風(fēng)格、經(jīng)驗豐富的設(shè)計團(tuán)隊。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營銷思維進(jìn)行網(wǎng)站設(shè)計、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
代碼實現(xiàn):
布局文件(Data-Binding模式),很簡單就是兩行文字
<?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>
從實例中我們可以看到右上角是有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個strings)
本案例我創(chuàng)建了4種語言。
好的,Menu的布局寫完了,接下來就是實現(xiàn)Menu功能,記住實現(xiàn)Menu就兩套代碼,一個 onCreateOptionsMenu , 另一個是 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); }
在這里,可以看到,我們自定義一個 updateViews() 方法,用來實現(xiàn)切換預(yù)言時界面的改變
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)); }
公布一個 語言判斷的類 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; } }
最后還要做的操作就是,自定義一個Application類,用來設(shè)定App的默認(rèn)語言(當(dāng)然了,要將這個Application應(yīng)用到Manifest中)
public class BaseApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(LocaleHelper.onAttach(base, "en")); } }
上述就是小編為大家分享的Android開發(fā)中怎么在App中實現(xiàn)一個內(nèi)語言切換功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:Android開發(fā)中怎么在App中實現(xiàn)一個內(nèi)語言切換功能
標(biāo)題URL:http://jinyejixie.com/article42/jjjehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、企業(yè)網(wǎng)站制作、App開發(fā)、Google、微信公眾號、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)