Android-单击按钮时手动更改应用程序区域设置 [英] Android - Change app locale manually on button click
问题描述
当用户希望使用更改语言"按钮在北印度语和英语之间切换时,我想更改应用程序的语言环境(语言)以进行编程更改.
I want to change the application's Locale (language) to change programmatically when user wants to switch between Hindi and English using change language button.
我有一个代码来更改语言,但是只有当我在setContentView()方法之前在活动的onCreate()中调用时,它才起作用.
I have a code to change language in place but it works only when I call in in the onCreate() of an activity before setContentView() method.
我们非常感谢您的帮助.
Any help is much appreciated.
推荐答案
在这里查看我的答案
例如,如果您希望您的应用程序同时支持英语和英语和阿拉伯字符串(除了默认字符串),您可以简单地另外创建两个资源目录称为/res/values-en
(用于英语strings.xml)和/res/values-ar
(用于阿拉伯字符串.xml).
for example if you want your application to support both English
and Arabic strings (in addition to the default strings),
you can simply create two additional
resource directories called /res/values-en
(for the English strings.xml) and
/res/values-ar
(for the Arabic strings.xml).
在 strings.xml
文件中,资源名称相同.
Within the strings.xml
files, the
resource names are the same.
例如,/res/values-en/strings.xml
文件可以看起来像这样:
For example, the /res/values-en/strings.xml
file could
look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
/res/values-ar/strings.xml文件如下所示:
Whereas, the /res/values-ar/strings.xml file would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">مرحبا في اللغة الإنجليزية</string>
</resources>
而且,/res/values-ur_IN/strings.xml文件对于urdu来说看起来像这样:
also , the /res/values-ur_IN/strings.xml file would look like this for urdu:
印度的ur_IN巴基斯坦的ur_PK
ur_IN for india ur_PK for pakisthan
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">انگریزی میں خوش!!</string>
</resources>
/res/layout目录中显示字符串的默认布局文件是指字符串由变量名@ string/hello决定,与哪种语言或目录无关字符串资源在其中.
A default layout file in the /res/layout directory that displays the string refers to the string by the variable name @string/hello, without regard to which language or directory the string resource is in.
Android操作系统确定哪个版本的在运行时加载的字符串(法语,英语或默认值).带有TextView控件的布局显示字符串可能看起来像这样:
The Android operating system determines which version of the string (French, English, or default) to load at runtime.A layout with a TextView control to display the string might look like this:
<?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>
可以通过常规方式以编程方式访问该字符串:
The string is accessed programmatically in the normal way:
String str = getString(R.string.hello);
要更改语言,您需要喜欢更改语言..
For change the language you need to like that change lang..
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();
}
});
这篇关于Android-单击按钮时手动更改应用程序区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!