我目前正在开发一个基于Yaroslav Berezanskyi代码支持 3 种语言的应用程序。这很棒,我可以在运行时更改应用程序语言,但是有一个问题让我感到困惑。不幸的是,当我想去 ActivityPreference 时,语言和资源仍然是英语,当我将应用程序语言更改为波斯语时,它不使用从右到左的资源,例如布局和字符串。Preference v7 有什么问题?当语言是英语和波斯语时,我从应用程序中附加了一些图像。预先感谢。
更改语言方法
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
活动偏好代码
public class ActivityPreference extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
if (savedInstanceState == null) {
// Display the fragment as the main content.
FragmentPreference fragmentPreference = FragmentPreference.newInstance();
fragmentPreference.setArguments(this.getIntent().getExtras());
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragmentPreference).commit();
}
}
}
片段偏好代码
public class FragmentPreference extends PreferenceFragmentCompat {
public static FragmentPreference newInstance(){
FragmentPreference fr = new FragmentPreference();
return fr;
}
@Override
public void onStart() {
super.onStart();
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
FragmentPreference.this.getActivity().getWindow().setLayout(width, height);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preference);
}
}