Android; PreferenceFragment语言与app不一样

Android ; PreferenceFragment language is not the same as the app

我正在开发一个应用程序来处理以后的语言更改偏好,但现在我的问题是我的应用程序将使用 phone 语言环境(在我的情况下是法语),但我的 R.xml.settings_prefs 语言是英语我翻译了我所有的字符串

strings.xml
values-fr/strings.xml
values-en/strings.xml

这是我的代码截图

SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_settings);
    // Display the fragment as the main content.
     getFragmentManager().beginTransaction()
    .replace(R.id.content_frame, new SettingsFragment())
    .commit();
}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.settings_prefs);

}

activity_settings.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  />
</RelativeLayout>

我做错了什么??

感谢帮助!!

您遇到的问题正是您所说的; "my app will use phone locale"。由于您没有专门设置另一个 Locale,它将使用默认值。要解决此问题,您可以在 SettingsActivity 中的 onCreate 中添加以下代码,然后再开始片段:

Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());

使用 en_US 语言环境应该是万无一失的,因为它必须始终可用。 ("locale data is not necessarily available for any of the locales pre-defined as constants in this class except for en_US, which is the only locale Java guarantees is always available" source: Android API).