Xamarin.Forms custom Android NavigationPageRenderer title and subtitle

南楼画角 提交于 2020-01-01 10:01:12

问题


Currently working on a project where I want to use AppCompat and have a requirement setting title and subtitle on most of the pages.

It doesn't work using AppCompat at all - neither setting the properties nor using a custom view.

When not using AppCompat both works as expected. The full source code is available here so just run the app if you're curious :)

using System.ComponentModel;
using Android.App;
using Android.Widget;
using App1.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
#if __APPCOMPAT__
using NavigationRenderer = Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer;
#else
using NavigationRenderer = Xamarin.Forms.Platform.Android.NavigationRenderer;
#endif

[assembly: ExportRenderer(typeof(NavigationPage), typeof(NavigationPageRenderer))]

namespace App1.Droid.Renderers
{
    public class NavigationPageRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);
            SetCustomView(e.NewElement.CurrentPage.GetType().Name);
        }

        private void SetCustomView(string view)
        {
            var activity = (Activity)Context;
#if __APPCOMPAT__
            var actionBar = ((FormsAppCompatActivity)Context).SupportActionBar;
#else
            var actionBar = activity.ActionBar;
#endif
            actionBar.Title = view;
            actionBar.Subtitle = " -> " + view;
            var abv = new LinearLayout(activity)
            {
                Orientation = Orientation.Vertical
            };

            var main = new TextView(activity)
            {
                Text = view,
            };
            main.SetTextColor(Color.Aqua.ToAndroid());
            main.SetPadding(4, 4, 2, 6);
            abv.AddView(main);
            abv.AddView(new TextView(activity)
            {
                Text = " -> " + view
            });
            actionBar.SetDisplayShowCustomEnabled(true);
            actionBar.CustomView = abv;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName.Equals("CurrentPage"))
            {
                SetCustomView(((NavigationPage)sender).CurrentPage.GetType().Name);
            }
        }
    }
}

Edit: Thanks @jimmgarr. Altered the code slightly to keep alternating between AppCompbat and "normal mode". The code is available here


回答1:


So it looks like the NavigationPage uses its own Toolbar instance. That's why setting the properties on SupportActionBar isn't doing anything.

I was able to get it working by overriding OnViewAdded() to get a reference to the new Toolbar when it's added:

public override void OnViewAdded(Android.Views.View child)
{
    base.OnViewAdded(child);

    if (child.GetType() == typeof(Support.Toolbar))
        toolbar = (Support.Toolbar)child; 
}

Then using the reference inside SetCustomView() to set only the Subtitle since the Title is already set automatically.

Here's the complete renderer class :)



来源:https://stackoverflow.com/questions/38499344/xamarin-forms-custom-android-navigationpagerenderer-title-and-subtitle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!