问题
I have Created Toolbar with back arrow, click is not working
toolbar = FindViewById<Toolbar>(Resource.Id.toolbar2);
toolbar.NavigationClick += Back;
private void Back(object sender, Toolbar.NavigationClickEventArgs e)
{
Finish();
}
回答1:
In your OnCreate method do this :
ActionBar.SetHomeButtonEnabled(true);
ActionBar.SetDisplayHomeAsUpEnabled(true);
Then override the OnOptionsItemSelected method like this.
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
Finish();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
If you are using Xamarin.Android.Support.v7 the Android.Resource.Id.Home should be Resource.Id.Home.
Also, you only need ActionBar.SetDisplayHomeAsUpEnabled(true); to show the Home button, no need for the ButtonEnabled call.
Something like this in OnCreate :
Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.toolbarID);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
And something like this in OnOptionsItemSelected:
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
Finish();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
来源:https://stackoverflow.com/questions/50620799/toolbar-back-click-is-not-working-in-xamarin-android