Android給app設置某種固定語言,不再跟隨系統語言變化

最近公司的一個項目中,app是有中文和英文兩種版本的,但是之後客戶要求只要英文,不要中文了,而且要將英文設置爲默認語言,一開始不知道怎麼做,就在網上找了很久的資料,沒找到太好的答案,最後找到一篇文章,按照文章中的方法試了一下,果然有效,步驟也很簡單,就3步:

1.寫一個類繼承Application,在onCreate方法中設置默認語言:

String languageToLoad  = "en";
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = getResources().getConfiguration();
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        config.locale = Locale.ENGLISH;
        getResources().updateConfiguration(config, metrics);

你需哪種語言就將languageToLoad設置爲相應的簡寫即可

2.在AndroidManifest文件中,application標籤下設置android:configChanges="locale"屬性

<application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:configChanges="locale"
        android:name="com.camera.wifi.app.MyApplication">

3.加權限:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

ok了,這樣就可以將app設置爲指定語言了,很簡單的,感謝作者給的解決方法!希望能對大家有幫助

轉載出處:https://blog.csdn.net/tangcheng_ok/article/details/7483993

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