我正在构建具有系统应用程序权限的启动器应用程序。我想让用户选择更改设备语言(不谈论应用程序语言)。
由于我的应用程序是启动器应用程序,因此用户只能移至WiFi列表屏幕,而不能移出该应用程序。我想给他们选择英语或西班牙语的选项,并为设备设置相同的语言。
我知道我们可以使用以下语言导航到语言设置
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");
startActivity(intent);
但这不是首选方法。
是否可以通过编程方式设置设备语言?
TIA
查看我的答案
对不起,我不懂西班牙语,所以我用阿拉伯语
例如,如果您希望您的应用程序同时支持英语和英语,和阿拉伯字符串(除了默认字符串),您可以简单地另外创建两个资源目录称为/res/values-en
(对于英语strings.xml),/res/values-ar
(用于阿拉伯字符串.xml)。
在strings.xml
文件中,资源名称相同。
例如,/res/values-en/strings.xml
文件可以看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
而/res/values-ar/strings.xml文件将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">مرحبا في اللغة الإنجليزية</string>
</resources>
另外,/ res / values-ur_IN / strings.xml文件对于urdu如下所示:
印度的ur_IN巴基斯坦的ur_PK
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">انگریزی میں خوش!!</string>
</resources>
/ res / layout目录中显示字符串的默认布局文件是指字符串由变量名@ string / hello决定,与语言或目录无关字符串资源在其中。
Android操作系统确定哪个版本的在运行时加载的字符串(法语,英语或默认值)。带有TextView控件的布局显示字符串可能看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" >
</LinearLayout>
以常规方式通过编程方式访问字符串:
String str = getString(R.string.hello);
要更改语言,您需要喜欢该更改语言。
btn_english.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});
btn_arbice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelecURdu), Toast.LENGTH_SHORT).show();
}
});
btn_urdu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ur_IN");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(HomeActivity.this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});