删除Xamarin.Android中的导航返回箭头
我有一个Xamarin.Forms应用程序。我想删除/隐藏导航栏中的后退箭头,但保留标题。我可以在iOS中使用我的删除Xamarin.Android中的导航返回箭头,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我有一个Xamarin.Forms应用程序。我想删除/隐藏导航栏中的后退箭头,但保留标题。我可以在iOS中使用我的NavigationPageRenderer中的以下代码来完成此操作: UINavigationBar.Appearance.BackIndicatorImage = new UIImage(); UINavigationBar.Appearance.BackIndicatorTransitionMaskImage = new UIImage(); 在Android中是否有类似的代
NavigationPageRenderer
中的以下代码来完成此操作:
UINavigationBar.Appearance.BackIndicatorImage = new UIImage();
UINavigationBar.Appearance.BackIndicatorTransitionMaskImage = new UIImage();
在Android中是否有类似的代码可以在渲染器或MainActivity中使用?我尝试了this.ActionBar.SetDisplayHomeAsUpEnabled(false)代码>在myMainActivity
中,但ActionBar
始终返回空值。以下是我的主要活动代码:
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应用程序中)
后退箭头只是后退按钮标题:NavigationPage.setbackbuttontile(此“\u25C3”)代码>
在我的内容页面中
:
public partial class HomeTabPage : ContentPage
{
public HomeTabViewModel vm;
public HomeTabPage()
{
InitializeComponent();
BindingContext = vm = new HomeTabViewModel(this);
NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetBackButtonTitle(this, "\u25C3");
}
}
解决方案:
您可以将自定义视图定义为特定平台(Android)上内容的导航栏
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
的内容
这类似于Page.HasBackButton=false
。您可能必须强制转换以下内容:ViewController.ParentViewController.NavigationItem.SetHidesBackButton(!((CustomContentPage)this.Element)。HasBackButton,false)如果您的意思是NavigationPage.SetHasBackButton(这个,false)代码>然后是的,我试过了,但没有工作当你调用NavigationPage.SetHasBackButton时你使用了哪个页面(这个,false)?如果你在NavigationPage上设置它,它不会做任何事情。你能提供你想要的效果截图吗?NavigationPage.SetHasBackButton(此选项为false);如果你在contentPage中调用它,应该可以。我在contentPage中调用过,谢谢。我会试试这个,过一会儿再给你回复。在一个测试项目上试过,它看起来很有效。谢谢快乐编码:)