我正在努力支持阿拉伯语从右到左的布局支持,但事实证明,你无法在Xamarin Forms中支持从右到左。
我甚至尝试了依赖服务,并自己调用了左边的布局。这是代码:
public CultureInfo SetLocale(string locale)
{
var netLocale = locale.Replace("_", "-");
var ci = new CultureInfo(netLocale);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr1)
{
// Change locale settings in the app.
var dm = Forms.Context.Resources.DisplayMetrics;
var conf = Forms.Context.Resources.Configuration;
conf.Locale = new Java.Util.Locale(locale);
Forms.Context.Resources.UpdateConfiguration(conf, dm);
if (locale == "en")
(Forms.Context as MainActivity).Window.DecorView.LayoutDirection = Android.Views.View.LayoutDirectionLtr;
else
(Forms.Context as MainActivity).Window.DecorView.LayoutDirection = Android.Views.View.LayoutDirectionRtl;
}
return ci;
}
答案 0 :(得分:1)
我希望我不会太晚,这里有一些链接:
there is layoutDirection和on this forum他们已经回答了这个问题:
要检测何时需要RTL,我使用这段代码(我发现需要反射,因为IsRightToLeft属性意外地无法访问):
bool rightToLeft = false;
PropertyInfo propertyInfo = thisCulture.TextInfo.GetType().GetRuntimeProperty("IsRightToLeft");
object value = propertyInfo?.GetValue(thisCulture.TextInfo);
if (value is bool)
rightToLeft = (bool) value;
之后,主要是切换一些属性的情况:
HorizontalOptions = rightToLeft ? LayoutOptions.EndAndExpand : LayoutOptions.StartAndExpand,
HorizontalTextAlignment = rightToLeft ? TextAlignment.End : TextAlignment.Start;
答案 1 :(得分:-7)
如果语言是阿拉伯语,您可以设置html的dir属性。
<html dir="rtl">