Another custom navigation bar (action bar) for different pages in android xamarin

I am trying to create a different action bar for different pages. For example, a few pages will have a title and a help button, while others should have a settings button.

How can this be done with a custom navigation renderer in Xamarin.Android?

Sample code for a custom navigation renderer:

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(CustomNavigationPageRenderer))]
namespace MobileAppSamples.Droid.CustomRenderers
{
    public class CustomNavigationPageRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);

            if (Element != null)
            {
                var actionBar = ((Activity)Context).ActionBar;

                Android.Widget.LinearLayout layout = new Android.Widget.LinearLayout(Context);
                layout.Orientation = Orientation.Horizontal;
                layout.WeightSum = 100;
                TextView title = new TextView(Context);
                title.Text = "sample";
                ViewGroup.LayoutParams textlp = new ViewGroup.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
                layout.AddView(title, textlp);
                actionBar.SetCustomView(layout, new ActionBar.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent));
                actionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowCustom | ActionBarDisplayOptions.ShowHome );
            }

        }




    }
}

      

+3


source to share


1 answer


The problem lies in your activity. Check out this code:



var bar = ((FormsApplicationActivity)Context).ActionBar;

      

0


source







All Articles