问题
How can I change the display language within an app? I want that users with different cultures can work with one scanner without changing the global culture of the device.
I have a laguage button with a click event in wich I call a method:
public void SetLocale(string language = "")
{
Locale locale = String.IsNullOrEmpty(language)
? new Locale("de-DE")
: new Locale(language);
Locale.Default = locale;
var config = new global::Android.Content.Res.Configuration();
config.Locale = locale;
var context = global::Android.App.Application.Context;
context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
}
But unfortunately nothing happens when I press the button.
The click event is:
_btnen.Click += delegate
{
SetLocale("en-GB");
};
回答1:
What can work for You, if the context can be forgotten (all property values can be lost), You can just force the Activity
to redraw itself.
btn.Click += delegate
{
SetLocale("en-GB");
this.Recreate(); //this line
}
回答2:
With help of @Rafael Stahl, I found following solution:
_btnen.Click += delegate
{
Java.Util.Locale.Default = new Locale("en", "GB");
Resources.Configuration.Locale = Java.Util.Locale.Default;
Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
Intent intent = new Intent(this, this.Class);
StartActivity(intent);
};
来源:https://stackoverflow.com/questions/41978802/change-display-language-within-an-xamarin-android-app