我尝试使用此方法。
protected override void AttachBaseContext(context @base)
{
Context con = LanguageManager.NewLocale(@base,
LanguageManager.SelectNewLanguage(@base));
base.AttachBaseContext(@base));
}
当我运行我的应用程序时,它会自动崩溃。
由于您提供的信息较少,所以如果您想创建一个可以更改语言的功能,在您的应用程序中。
1。<< [LanguageManager类
internal class LanguageManager
{
private const string MYLANGUAGE = "myLanguage";
private const string MYPREF = "myPreference";
public static Context LoadLanguage(Context context)
{
var loadedLanguage = GetLanguage(context, Locale.Default.Language);
return ChangeLanguage(context, loadedLanguage);
}
public static Context ChangeLanguage(Context context, string language)
{
SaveLanguage(context, language);
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
return ChangeForAPI24(context, language);
}
return ChangeForLegacy(context, language);
}
private static string GetLanguage(Context context, string Language)
{
var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);
return privatePreference.GetString(MYLANGUAGE, Language);
}
private static void SaveLanguage(Context context, string language)
{
var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);
var editor = privatePreference.Edit();
editor.PutString(MYLANGUAGE, language);
editor.Apply();
}
private static Context ChangeForAPI24(Context context, string language)
{
// for api >= 24
var locale = new Locale(language);
Locale.Default = locale;
var configuration = context.Resources.Configuration;
configuration.SetLocale(locale);
configuration.SetLayoutDirection(locale);
return context.CreateConfigurationContext(configuration);
}
private static Context ChangeForLegacy(Context context, string language)
{
var locale = new Locale(language);
Locale.Default = locale;
var resources = context.Resources;
var configuration = resources.Configuration;
configuration.Locale = locale;
resources.UpdateConfiguration(configuration, resources.DisplayMetrics);
return context;
}
}
2。,其中包括一个TextView和一个按钮;3.inMainActivity
BaseActivity
public class BaseActivity: AppCompatActivity
{
protected override void AttachBaseContext(Context @base)
{
base.AttachBaseContext(LanguageManager.LoadLanguage(@base));
}
}
4.in中可以设置语言,与您的语言相似的axmlSettingActivity
public class SettingActivity : BaseActivity, CompoundButton.IOnCheckedChangeListener
{
private Switch swCh;
private Switch swEn;
private Switch swTh;
private Bundle s;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.setting);
// Create your application here
initView();
}
private void initView()
{
Button back = FindViewById<Button>(Resource.Id.back);
back.Click += delegate { Finish(); };
swCh = FindViewById<Switch>(Resource.Id.switch1);
swEn = FindViewById<Switch>(Resource.Id.switch2);
swTh = FindViewById<Switch>(Resource.Id.switch3);
var s = GetSharedPreferences("myPreference", FileCreationMode.Private).GetString( "myLanguage", Locale.Default.Language);
switch (s)
{
case "ch":
swCh.Checked = true;
break;
case "en":
swEn.Checked = true;
break;
case "th":
swTh.Checked = true;
break;
}
swCh.SetOnCheckedChangeListener(this);
swEn.SetOnCheckedChangeListener(this);
swTh.SetOnCheckedChangeListener(this);
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
if (isChecked)
{
switch (buttonView.Id)
{
case Resource.Id.switch1:
swEn.Checked = false;
swTh.Checked = false;
LanguageManager.ChangeLanguage(this, "ch");
break;
case Resource.Id.switch2:
swCh.Checked = false;
swTh.Checked = false;
LanguageManager.ChangeLanguage(this, "en");
break;
case Resource.Id.switch3:
swEn.Checked = false;
swCh.Checked = false;
LanguageManager.ChangeLanguage(this, "th");
break;
}
//restart application to change language
Intent intent = new Intent(this, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
您可以参考here