Android 当区域设置更改时,ActivityReference不使用从右到左的资源
我目前正在开发一个基于代码支持3种语言的应用程序。这很好,我可以在运行时更改应用程序语言,但有一个问题让我感到困惑。不幸的是,当我想转到ActivityReference时,语言和资源仍然是英语,当我将应用程序语言更改为波斯语时,它不会使用从右到左的资源,例如布局和字符串。首选项v7有什么问题?我从应用程序中附加了一些图像,当语言为英语和波斯语时。先谢谢你 更改语言方法Android 当区域设置更改时,ActivityReference不使用从右到左的资源,android,android-preferences,preference-v7,Android,Android Preferences,Preference V7,我目前正在开发一个基于代码支持3种语言的应用程序。这很好,我可以在运行时更改应用程序语言,但有一个问题让我感到困惑。不幸的是,当我想转到ActivityReference时,语言和资源仍然是英语,当我将应用程序语言更改为波斯语时,它不会使用从右到左的资源,例如布局和字符串。首选项v7有什么问题?我从应用程序中附加了一些图像,当语言为英语和波斯语时。先谢谢你 更改语言方法 private static Context updateResources(Context context, String
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);
}
}
片段首选项代码
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);
}
}
ActivityReference不支持Rtl@FarshadAsgharzadeh必须有一种方法强制ActivityReference使用rtl资源。我去年尝试过,但失败了。您必须为创建自己的自定义活动preference@FarshadAsgharzadeh它也不使用rtl字符串资源。它没有意义。您是否尝试在设置区域设置后刷新活动?