更改导航栏上的图标-Xamarin.Forms Android [英] Change icon on Navigation bar - Xamarin.Forms Android

查看:361
本文介绍了更改导航栏上的图标-Xamarin.Forms Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Xamarin.Forms-android中设置不同的图标.

How can I set different icons in Xamarin.Forms - android.

一个用于应用程序,商店,用户屏幕,另一个用于导航页面.

One for the application, play store, user screen and other for the navigation page.

我更新了Project.Droid/MainActivity.cs文件:

I updated Project.Droid/MainActivity.cs file:

[Activity(Label = "MyAppName", Icon =  "MyIconName", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

但是这样可以更改两个图标!

But this way change two icons!!

以其他方式,我更新了ProjectForms/App.cs:

Other way that I did, I updated ProjectForms/App.cs:

        Current.Resources = new ResourceDictionary();
        Current.Resources.Add("UlycesColor", Color.FromRgb(121, 248, 81));
        var navigationStyle = new Style(typeof(NavigationPage));
        var barTextColorSetter = new Setter { Property = NavigationPage.BarTextColorProperty, Value = Color.Black };
        var barBackgroundColorSetter = new Setter { Property = NavigationPage.BarBackgroundColorProperty, Value = Color.White };
        var barIcon = new Setter { Property = NavigationPage.IconProperty, Value = "newIcon.png" };

        navigationStyle.Setters.Add(barTextColorSetter);
        navigationStyle.Setters.Add(barBackgroundColorSetter);
        navigationStyle.Setters.Add(barIcon);
        Current.Resources.Add(navigationStyle);

但是不行!

有什么主意吗???

推荐答案

您可以使用自定义渲染器:

You could do it with custom renderer:

[assembly:ExportRenderer (typeof(NavigationPage), typeof(NavigationPageRenderer))]
namespace SuperForms.Samples.Droid
{
    public class NavigationPageRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);

            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon(Resource.Drawable.newIcon);
        }
    }
}

将图标添加到Resources/drawable文件夹:

外观如何:

这篇关于更改导航栏上的图标-Xamarin.Forms Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆