Android:如何让 App Locale 独立于 System Locale 工作?

Luo*_*ong 5 java android locale android-studio

我有一个与显示语言相关的问题。我能够独立于操作系统系统更改应用程序内部的语言(英语为“en”,日语为“ja”)。

但是,问题是当应用程序处于“ja”时,如果用户手动更改系统语言(不是“en”或“ja”),我的应用程序会自动将语言更改为默认语言(“en”)。我想让我的应用程序的语言环境独立,无论用户手动更改语言,应用程序的语言仍然与他们注销时相同。

编辑

有一些有用的链接,但它们仍然无法解决我的问题。例如: 在 Android 中以编程方式更改语言

你能给我任何建议吗?

先感谢您!

Suh*_*Lee 0

试试这个

import java.util.Locale; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.content.res.Resources; 
import android.util.DisplayMetrics; 

public void setLocale(String lang) { 
    myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, AndroidLocalize.class); 
    startActivity(refresh); 
    finish();
} 
Run Code Online (Sandbox Code Playgroud)

添加:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    newConfig.setLocale(yourLocale);
    super.onConfigurationChanged(newConfig);
}
Run Code Online (Sandbox Code Playgroud)

添加(2):

您必须进行设置android:configChanges="layoutDirection|locale"才能onConfigurationChanged()在更改时触发Locale
我无法完全理解为什么会发生这种情况,也许有一些RTL语言......