无法在Android上强制执行RTL

When using FolowDirection with RightToLeft for elements in XAML, it works fine.

但是,我要默认设置并在整个应用程序中强制使用。

Followed the docs but nothing is happening, setting Window.DecorView.LayoutDirection does not seem to do anything.

I have also set: <application android:label="App.Android" android:supportsRtl="true"></application>

这是我的Android项目代码:

protected override void OnCreate(Bundle savedInstanceState)
{
    Window.DecorView.LayoutDirection = Android.Views.LayoutDirection.Rtl;

    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
评论
  • qet
    qet 回复
    我想默认设置并在整个应用程序中强制使用。

    首先,我们应该知道应用程序的方向是基于设备的语言和区域的,因此您需要确保语言和区域的设置是RTL(如阿拉伯语)。

    您可以在后面的代码中检查它

    var flowDirection = Device.FlowDirection; // this is a readonly property 
    

    如果您确实想将应用设置为RTL而不要更改系统设置。您可以创建一个基本ContentPage并设置它的FlowDirection。该值将被其所有子级继承(除非您覆盖它)。

    public partial class BaseContentPage : ContentPage
    {
      public BaseContentPage()
      {
        InitializeComponent();
        FlowDirection = FlowDirection.RightToLeft;
      }
    }
    

    如果要让特定元素(如标签)保持LTR,则只需将标签上的FlowDirection属性设置为LeftToRight。