大家。 :)我有应用程序与多布局,我想在运行该应用更改语言。 我发现这个代码,以帮助我改变语言
using Android.Content;
using Android.OS;
using Java.Util;
namespace RuntimeAppLanguage
{
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;
}
}
}
我试图使它在这个布局工作,但我结束了环:(
当用户检查英语阿拉伯语开关转到取消等什么,我试图做的是。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="386.5dp"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:background="#ffb39ddb">
<Button
android:text="Back"
android:layout_width="80.0dp"
android:layout_height="match_parent"
android:id="@+id/back"
android:background="#ffb39ddb"
android:drawableLeft="@drawable/arrowangle"
android:textSize="18dp" />
<TextView
android:text="Languagus"
android:layout_width="222.0dp"
android:textColor="#FF010101"
android:textSize="20dp"
android:layout_height="match_parent"
android:id="@+id/textView1"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="134.5dp"
android:id="@+id/linearLayout2"
android:layout_marginTop="0.0dp"
android:layout_below="@id/linearLayout1"
android:background="#ffbdbdbd"
>
<Switch
android:layout_width="match_parent"
android:layout_height="59.0dp"
android:id="@+id/switch1"
android:background="#ffffffff"
android:layout_marginTop="5dp"
android:text=" English"
android:textSize="25dp"
android:textStyle="normal"
android:checked="false" />
<Switch
android:layout_width="match_parent"
android:layout_height="59.0dp"
android:id="@+id/switch2"
android:background="#ffffffff"
android:layout_marginTop="5dp"
android:text=" Arabic"
android:textSize="25dp" />
</LinearLayout>
</RelativeLayout>
请帮我 :(