删除Xamarin.Android中的导航后退箭头

问题描述 投票:1回答:1

我有一个Xamarin.Forms应用程序。我想删除/隐藏导航栏中的后退箭头,但保留标题。我能够在iOS中使用NavigationPageRenderer中的以下代码来完成它:

UINavigationBar.Appearance.BackIndicatorImage = new UIImage();
UINavigationBar.Appearance.BackIndicatorTransitionMaskImage = new UIImage();

我可以在我的渲染器或MainActivity中使用Android中的任何等效代码吗?我在this.ActionBar.SetDisplayHomeAsUpEnabled(false);中尝试了MainActivity,但是ActionBar总是返回null。以下是我的MainActivity代码:

public class MainActivity : FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
       TabLayoutResource = Resource.Layout.Tabbar;
       ToolbarResource = Resource.Layout.Toolbar;

       base.OnCreate(bundle);

       global::Xamarin.Forms.Forms.Init(this, bundle);
       LoadApplication(new App());
       if (Window != null)
       {
          Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
       }

       this.ActionBar.SetDisplayHomeAsUpEnabled(false);

     }
}

我希望我的导航栏看起来像下面的图像(这是在我的iOS应用程序中)。

enter image description here

后退箭头只是后退按钮标题:NavigationPage.SetBackButtonTitle(this, "\u25C3");

在我的ContentPage

public partial class HomeTabPage : ContentPage
{
   public HomeTabViewModel vm;

   public HomeTabPage()
   {
      InitializeComponent();
      BindingContext = vm = new HomeTabViewModel(this);
      NavigationPage.SetHasBackButton(this, false);
      NavigationPage.SetBackButtonTitle(this, "\u25C3");
   }
}
xamarin xamarin.forms xamarin.android
1个回答
1
投票

解决方案:您可以在特定平台(Android)上将自定义视图定义为内容的navigationBar。请参阅以下代码。

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();

        if(Device.RuntimePlatform=="Android")
        {
            NavigationPage.SetHasBackButton(this, false);
            NavigationPage.SetTitleView(this, SetBackView("Title", "back"));
        }


    }

    private void BackButton_Clicked(object sender, EventArgs e)
    {
        Navigation.PopAsync();
    }

    StackLayout SetBackView (string title,string backButtonContent)
    {
        Button backButton = new Button()
        {

            Text = backButtonContent,
            TextColor = Color.White,
            FontAttributes=FontAttributes.None,
            BackgroundColor = Color.Transparent,
            Margin = new Thickness(-20,0,0,0),
        };
        backButton.Clicked += BackButton_Clicked;

        StackLayout stackLayout = new StackLayout
        {

            Children = {

                backButton,

                new Label{

                    HorizontalTextAlignment=TextAlignment.Center,
                    VerticalTextAlignment=TextAlignment.Center,
                    Text=title,
                    TextColor=Color.White,
                    BackgroundColor=Color.Transparent,
                },

            },
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Orientation = StackOrientation.Horizontal,
        };

        return stackLayout;
    }

}

效果如下,您可以根据需要设置页面标题和backButton的内容。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.