使用Xamarin Forms Shell时,是否可以在Android上将页面标题居中?

Ala*_*an2 4 xamarin xamarin.forms

我最近更改为Xamarin Forms,并注意到标题不在Android设备页面的顶部居中。

有办法可以做到吗?

这是标题的示例:

在此处输入图片说明

Nik*_*hil 8

在这种情况下,您将必须实现ShellRenderer,因为您拥有Xamarin.Forms Shell Project。

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Support.V4.Widget;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Widget;
using Japanese.Droid.CustomRenderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Toolbar = Android.Support.V7.Widget.Toolbar;

[assembly: ExportRenderer(typeof(Xamarin.Forms.Shell), typeof(MyShellRenderer))]
namespace MyProject.Droid.CustomRenderers
{
    public class MyShellRenderer : ShellRenderer
    {
        public MyShellRenderer(Context context) : base(context)
        {
        }

        protected override IShellToolbarAppearanceTracker CreateToolbarAppearanceTracker()
        {
            return new MyShellToolbarAppearanceTracker(this);
        }

        protected override IShellToolbarTracker CreateTrackerForToolbar(Toolbar toolbar)
        {
            return new MyShellToolbarTracker(this, toolbar, ((IShellContext)this).CurrentDrawerLayout);
        }
    }

    public class MyShellToolbarAppearanceTracker : ShellToolbarAppearanceTracker
    {
        public MyShellToolbarAppearanceTracker(IShellContext context) : base(context)
        {
        }

        public override void SetAppearance(Android.Support.V7.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
        {
            base.SetAppearance(toolbar, toolbarTracker, appearance);
            //Change the following code to change the icon of the Header back button.

            toolbar?.SetNavigationIcon(Resource.Drawable.back);
        }
    }

    public class MyShellToolbarTracker : ShellToolbarTracker
    {
        public MyShellToolbarTracker(IShellContext shellContext, Toolbar toolbar, DrawerLayout drawerLayout) : base(shellContext, toolbar, drawerLayout)
        {
        }

        protected override void UpdateTitleView(Context context, Toolbar toolbar, View titleView)
        {
            base.UpdateTitleView(context, toolbar, titleView);
            for (int index = 0; index < toolbar.ChildCount; index++)
            {
                if (toolbar.GetChildAt(index) is TextView)
                {
                    var title = toolbar.GetChildAt(index) as TextView;
                    //Change the following code to change the font size of the Header title.
                    title.SetTextSize(ComplexUnitType.Sp, 20);
                    toolbar.SetTitleMargin(MainActivity.displayMetrics.WidthPixels / 4 - Convert.ToInt32(title.TextSize) - title.Text.Length / 2, 0, 0, 0);
                }
            }  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是MainActivity.cs的代码

public class MainActivity : FormsAppCompatActivity
{
    public static DisplayMetrics displayMetrics;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        Window.AddFlags(WindowManagerFlags.Fullscreen);

        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        displayMetrics = new DisplayMetrics();
        WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics);

        LoadApplication(new App());

        if (Window != null) Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
        if (isPhone(this)) RequestedOrientation = ScreenOrientation.Portrait;

    }
}
Run Code Online (Sandbox Code Playgroud)


Rou*_*hof 6

您可以使用TitleView

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TitleViewSample"
             x:Class="TitleViewSample.MainPage">

    <NavigationPage.TitleView>
        <Label Text="Hello World" HorizontalTextAlignement="Center"/>
    </NavigationPage.TitleView>

    <ContentPage.Content>    
        <StackLayout>
            <!-- Place new controls here -->
            <Label Text="Welcome to Xamarin.Forms!" 
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>

</ContentPage>
Run Code Online (Sandbox Code Playgroud)

https://www.andrewhoefling.com/Blog/Post/xamarin-forms-title-view-a-powerful-navigation-view

  • 有趣的是,我的不在中心。看来后退按钮把它扔掉了。 (2认同)