最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)應(yīng)用內(nèi)置語言切換功能

 更新時間:2017年02月09日 09:09:03   作者:cekiasoo  
這篇文章主要為大家詳細介紹了Android實現(xiàn)應(yīng)用內(nèi)置語言切換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、需求

有時候應(yīng)用需要在內(nèi)部切換語言但又不影響系統(tǒng)的語言,比如是應(yīng)用現(xiàn)在是中文的,系統(tǒng)語言也是中文的,我把應(yīng)用的切換成英文顯示后系統(tǒng)語言還是中文的,系統(tǒng)語言切換后也不會被改變,還有就是有些機的系統(tǒng)是被改造精簡過的,比如有些國產(chǎn)機的系統(tǒng)的語言就被精簡剩中文和英文。支付寶、微信、Top Story都有在應(yīng)用內(nèi)部設(shè)置語言切換這樣的功能。

二、實現(xiàn)效果

先看看實現(xiàn)效果吧。

三、實現(xiàn)

(一)添加多種語言的資源文件夾及文件

我這默認是英語再添加了個俄文(Google翻譯的)和中文。

values/strings.xml

<resources> 
  <string name="app_name">SwitchLanguage</string> 
  <string name="helloworld">Hello World!</string> 
  <string name="language">Eng</string> 
  <string name="english">English</string> 
  <string name="chinese">中文</string> 
  <string name="russian">русский</string> 
  <string name="secondact">Second Activity</string> 
</resources> 

values-ru/strings.xml

<resources> 
  <string name="app_name">Переключение язык</string> 
  <string name="helloworld">привет мир!</string> 
  <string name="language">русский</string> 
  <string name="secondact">второй активность</string> 
</resources> 

values-zh/strings.xml

<resources> 
  <string name="app_name">切換語言</string> 
  <string name="helloworld">你好 世界!</string> 
  <string name="language">中文</string> 
  <string name="secondact">第二屏</string> 
</resources> 

(二)布局文件
activity_main.xml

默認標題欄的文字是切換語言后是不會被改變的,使用Toobar替換掉就可以了。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  android:id="@+id/activity_main" 
  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" 
  tools:context="com.ce.switchlanguage.MainActivity" 
  xmlns:app="http://schemas.android.com/apk/res-auto"> 
  <android.support.design.widget.AppBarLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:theme="@style/AppTheme.AppBarOverlay"> 
 
    <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:title="@string/app_name"/> 
 
  </android.support.design.widget.AppBarLayout> 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/helloworld"/> 
  <Button 
    android:id="@+id/change" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/language"/> 
</LinearLayout> 

styles.xml

設(shè)置語言后需要重啟下activity,啟動會有個效果,使用windowDisablePreview屏蔽掉它。

<resources> 
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowDisablePreview">true</item> 
  </style> 
  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> 
  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark"/> 
</resources> 

main_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:app="http://schemas.android.com/apk/res-auto"> 
  <item android:id="@+id/chinese" android:title="@string/chinese" app:showAsAction="never" /> 
  <item android:id="@+id/english" android:title="@string/english" app:showAsAction="never" /> 
  <item android:id="@+id/russian" android:title="@string/russian" app:showAsAction="never" /> 
</menu> 

(三)LocaleUtils

package com.ce.switchlanguage; 
 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.res.Configuration; 
import android.os.Build; 
import android.util.DisplayMetrics; 
 
import com.google.gson.Gson; 
 
import java.util.Locale; 
 
public class LocaleUtils { 
  /** 
   * 中文 
   */ 
  public static final Locale LOCALE_CHINESE = Locale.CHINESE; 
  /** 
   * 英文 
   */ 
  public static final Locale LOCALE_ENGLISH = Locale.ENGLISH; 
  /** 
   * 俄文 
   */ 
  public static final Locale LOCALE_RUSSIAN = new Locale("ru"); 
  /** 
   * 保存SharedPreferences的文件名 
   */ 
  private static final String LOCALE_FILE = "LOCALE_FILE"; 
  /** 
   * 保存Locale的key 
   */ 
  private static final String LOCALE_KEY = "LOCALE_KEY"; 
 
  /** 
   * 獲取用戶設(shè)置的Locale 
   * @param pContext Context 
   * @return Locale 
   */ 
  public static Locale getUserLocale(Context pContext) { 
    SharedPreferences _SpLocale = pContext.getSharedPreferences(LOCALE_FILE, Context.MODE_PRIVATE); 
    String _LocaleJson = _SpLocale.getString(LOCALE_KEY, ""); 
    return jsonToLocale(_LocaleJson); 
  } 
  /** 
   * 獲取當(dāng)前的Locale 
   * @param pContext Context 
   * @return Locale 
   */ 
  public static Locale getCurrentLocale(Context pContext) { 
    Locale _Locale; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多語言設(shè)置獲取頂部的語言 
      _Locale = pContext.getResources().getConfiguration().getLocales().get(0); 
    } else { 
      _Locale = pContext.getResources().getConfiguration().locale; 
    } 
    return _Locale; 
  } 
  /** 
   * 保存用戶設(shè)置的Locale 
   * @param pContext Context 
   * @param pUserLocale Locale 
   */ 
  public static void saveUserLocale(Context pContext, Locale pUserLocale) { 
    SharedPreferences _SpLocal=pContext.getSharedPreferences(LOCALE_FILE, Context.MODE_PRIVATE); 
    SharedPreferences.Editor _Edit=_SpLocal.edit(); 
    String _LocaleJson = localeToJson(pUserLocale); 
    _Edit.putString(LOCALE_KEY, _LocaleJson); 
    _Edit.apply(); 
  } 
  /** 
   * Locale轉(zhuǎn)成json 
   * @param pUserLocale UserLocale 
   * @return json String 
   */ 
  private static String localeToJson(Locale pUserLocale) { 
    Gson _Gson = new Gson(); 
    return _Gson.toJson(pUserLocale); 
  } 
  /** 
   * json轉(zhuǎn)成Locale 
   * @param pLocaleJson LocaleJson 
   * @return Locale 
   */ 
  private static Locale jsonToLocale(String pLocaleJson) { 
    Gson _Gson = new Gson(); 
    return _Gson.fromJson(pLocaleJson, Locale.class); 
  } 
  /** 
   * 更新Locale 
   * @param pContext Context 
   * @param pNewUserLocale New User Locale 
   */ 
  public static void updateLocale(Context pContext, Locale pNewUserLocale) { 
    if (needUpdateLocale(pContext, pNewUserLocale)) { 
      Configuration _Configuration = pContext.getResources().getConfiguration(); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        _Configuration.setLocale(pNewUserLocale); 
      } else { 
        _Configuration.locale =pNewUserLocale; 
      } 
      DisplayMetrics _DisplayMetrics = pContext.getResources().getDisplayMetrics(); 
      pContext.getResources().updateConfiguration(_Configuration, _DisplayMetrics); 
      saveUserLocale(pContext, pNewUserLocale); 
    } 
  } 
  /** 
   * 判斷需不需要更新 
   * @param pContext Context 
   * @param pNewUserLocale New User Locale 
   * @return true / false 
   */ 
  public static boolean needUpdateLocale(Context pContext, Locale pNewUserLocale) { 
    return pNewUserLocale != null && !getCurrentLocale(pContext).equals(pNewUserLocale); 
  } 
} 

Locale工具類,這里我用SharedPreferences來保存所設(shè)置的Locale,Locale是實現(xiàn)了Serializable的。

(四)Activity

package com.ce.switchlanguage; 
 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
 
public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar _Toolbar =(Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(_Toolbar); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main_menu,menu); 
    return true; 
  } 
 
  @Override 
  public boolean onOptionsItemSelected(MenuItem item) { 
    int _ItemId=item.getItemId(); 
    switch (_ItemId) { 
      case R.id.chinese: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_CHINESE)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_CHINESE); 
          restartAct(); 
        } 
        break; 
      case R.id.english: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_ENGLISH)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_ENGLISH); 
          restartAct(); 
        } 
        break; 
      case R.id.russian: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_RUSSIAN)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_RUSSIAN); 
          restartAct(); 
        } 
    } 
    return true; 
  } 
 
  /** 
   * 重啟當(dāng)前Activity 
   */ 
  private void restartAct() { 
    finish(); 
    Intent _Intent = new Intent(this, MainActivity.class); 
    startActivity(_Intent); 
    //清除Activity退出和進入的動畫 
    overridePendingTransition(0, 0); 
  } 
} 

這里只有一個Activity所以切換的時候重啟下當(dāng)前Activity就好了,棧里還有其他Activity的自己再處理吧。

(五)Application

package com.ce.switchlanguage; 
 
import android.app.Application; 
import android.content.res.Configuration; 
import android.os.Build; 
 
import java.util.Locale; 
 
public class MyApplication extends Application { 
  @Override 
  public void onCreate() { 
    super.onCreate(); 
    Locale _UserLocale=LocaleUtils.getUserLocale(this); 
    LocaleUtils.updateLocale(this, _UserLocale); 
  } 
 
  @Override 
  public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    Locale _UserLocale=LocaleUtils.getUserLocale(this); 
    //系統(tǒng)語言改變了應(yīng)用保持之前設(shè)置的語言 
    if (_UserLocale != null) { 
      Locale.setDefault(_UserLocale); 
      Configuration _Configuration = new Configuration(newConfig); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        _Configuration.setLocale(_UserLocale); 
      } else { 
        _Configuration.locale =_UserLocale; 
      } 
      getResources().updateConfiguration(_Configuration, getResources().getDisplayMetrics()); 
    } 
  } 
} 

在Application onCreate的時候更新下,在系統(tǒng)語言改變的時候也要保持之前設(shè)置的語言不變。

源碼地址:Android應(yīng)用內(nèi)置語言切換

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spinner在Dialog中的使用效果實例代碼詳解

    Spinner在Dialog中的使用效果實例代碼詳解

    這篇文章主要介紹了Spinner在Dialog中的使用效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • android之計時器(Chronometer)的使用以及常用的方法

    android之計時器(Chronometer)的使用以及常用的方法

    在Android的SDK中,為我們提供了一個計時器,這個計時器稱為Chronometer,我們可以成它為Android的一個組件,同時它也具備自己獨有的方法
    2013-01-01
  • Android仿抖音右滑清屏左滑列表功能的實現(xiàn)代碼

    Android仿抖音右滑清屏左滑列表功能的實現(xiàn)代碼

    這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • android通知欄的實現(xiàn)方法分析

    android通知欄的實現(xiàn)方法分析

    這篇文章主要介紹了android通知欄的實現(xiàn)方法,結(jié)合實例形式分析了系統(tǒng)Notification及自定義Notification兩種實現(xiàn)技巧與相關(guān)操作步驟,需要的朋友可以參考下
    2016-08-08
  • Android自定義動態(tài)壁紙開發(fā)詳解

    Android自定義動態(tài)壁紙開發(fā)詳解

    這篇文章主要為大家詳細介紹了Android自定義動態(tài)壁紙開發(fā),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Flutter實現(xiàn)簡單的下載按鈕動畫

    Flutter實現(xiàn)簡單的下載按鈕動畫

    我們在app的開發(fā)過程中經(jīng)常會用到一些表示進度類的動畫效果,比如一個下載按鈕,那么在flutter中一個下載按鈕的動畫應(yīng)該如何制作呢,一起來看看吧
    2023-05-05
  • Android布局之絕對布局AbsoluteLayout詳解

    Android布局之絕對布局AbsoluteLayout詳解

    這篇文章主要為大家詳細介紹了Android布局之絕對布局AbsoluteLayout的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 內(nèi)存泄漏檢測工具LeakCanary源碼解析

    內(nèi)存泄漏檢測工具LeakCanary源碼解析

    這篇文章主要為大家介紹了內(nèi)存泄漏檢測工具LeakCanary源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Android使用TextView跑馬燈效果

    Android使用TextView跑馬燈效果

    這篇文章主要為大家詳細介紹了Android使用TextView跑馬燈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Flutter質(zhì)感設(shè)計之列表項

    Flutter質(zhì)感設(shè)計之列表項

    這篇文章主要為大家詳細介紹了Flutter質(zhì)感設(shè)計之列表項,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論

新晃| 从江县| 永顺县| 阿克苏市| 全州县| 凯里市| 乡城县| 随州市| 大厂| 漯河市| 宜宾市| 新源县| 大安市| 宣汉县| 宁夏| 漳浦县| 台山市| 保定市| 翼城县| 乐昌市| 彰化市| 保定市| 阿拉尔市| 灵石县| 外汇| 金华市| 晋州市| 桐庐县| 临泉县| 东乡族自治县| 岑巩县| 炉霍县| 茶陵县| 翼城县| 曲水县| 竹溪县| 黄浦区| 遵化市| 英山县| 华蓥市| 汾西县|