簡體   English   中英

在 Xamarin Forms 中居中對齊工具欄項目

[英]Center align toolbar items in Xamarin Forms

我有一個帶有工具欄的內容頁面,如下所示

內容頁

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChartList : ContentPage
{
    public ChartList ()
    {
        InitializeComponent ();

        //public class ToolbarItem : MenuItem
        ToolbarItem settings = new ToolbarItem
        {
            Icon = "icon.png",
            Text = "Settings",
            Command = new Command(this.ShowHomePage),
        };

        this.ToolbarItems.Add(settings);
    }

    private void ShowHomePage()
    {
        this.Navigation.PushAsync(new MainPage());
    }
}

應用程序.Xaml.cs

    public App()
    {
        InitializeComponent();

        ContentPage p = new MyHomeScreen2.MainPage();
        MainPage = new NavigationPage(p)
        {
            BarBackgroundColor = Color.Purple,
            BarTextColor = Color.White
        };
    }

我需要在工具欄的中心對齊圖標。 如何在 Xamarin Forms 中做到這一點?

在此處輸入圖片說明

我知道在問題Shell可能尚未引入時,但現在如果您的應用程序基於 Shell,您甚至可以在沒有自定義渲染器的情況下輕松實現它,並且還可以使用Shell.TitleView作為 Microsoft 示例自定義您的工具欄:

<ContentPage ...>
    <Shell.TitleView>
        <Image Source="xamarin_logo.png"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />
    </Shell.TitleView>
    ...
</ContentPage>

由於 Xamarin.forms標題視圖的內容水平對齊中的錯誤,它可能不會完全居中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM