更改应用程序的区域设置时,TextView不会更改 [英] TextView not changing when changing Locale of app
问题描述
从onOptionsItemSelected
中选择选项时,我试图更改应用程序的locale
.
I am trying to change the locale
of application when selecting options from the onOptionsItemSelected
.
当我尝试这样做时,语言会在同一菜单中成功更改.
When I try to do so, the language gets successfully changed in the same menu.
但是,布局中出现的TextView
中的text
不变.
However, the text
in the TextView
present in the layout doesn't change.
感谢您的帮助.
我的MainActivity是:
My MainActivity is:
public class MainActivity extends AppCompatActivity {
TextView textView;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_english) {
Toast.makeText(getApplicationContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
return true;
} else if (id == R.id.action_nepal) {
Toast.makeText(getApplicationContext(),
"You have selected Nepali", Toast.LENGTH_SHORT)
.show();
setLocale("ne");
return true;
}
return super.onOptionsItemSelected(item);
}
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getBaseContext().getResources().updateConfiguration(conf,
getBaseContext().getResources().getDisplayMetrics());
invalidateOptionsMenu();
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}
}
我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.languagesupport">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:configChanges="locale"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
还有我的两个字符串文件:
And both my strings file :
对于英语:
<resources>
<string name="app_name">languageChange</string>
<string name="sample_text">Hello World</string>
<string name="action_settings">Settings</string>
<string name="english">English</string>
<string name="nepal">Nepal</string>
</resources>
对于尼泊尔人:
<resources>
<string name="app_name">भीमदत्त नगरपालिका</string>
<string name="action_settings">गृहपृष्ठ</string>
<string name="sample_text">" विकास क्षेत्रमा पर्ने महाकाली अञ्चल), कञ्चनपुर जिल्लाको स"</string>
<string name="english">अंग्रेजी</string>
<string name="nepal">नेपाल</string>
</resources>
推荐答案
有两种不同的方法:
1.没有重新启动活动:
-
在清单中的
<activity>
中设置android:configChanges="locale"
按如下所示更改您的setLocale:
Change your setLocale as follows:
private void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
invalidateOptionsMenu();
onConfigurationChanged(conf);//Add this line
}
覆盖onConfigurationChanged()
:
@Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
textView.setText(<your-text>);
//Any other UI text to change
}
这将确保您的区域设置正确更改.
This will ensure your locale is changed correctly.
2.重新创建活动:
代替
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
致电
recreate();
这将导致使用新实例重新创建此活动.
This will cause this Activity to be recreated with a new instance.
这篇关于更改应用程序的区域设置时,TextView不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!