简体   繁体   English

无法将多色图像添加到 Xamarin Forms 中的 TabbedPage 导航栏

[英]Can't add multi-colour images to TabbedPage navigation bar in Xamarin Forms

So when I designed the navigation bar for my Android application I made it look like this:所以当我为我的 Android 应用程序设计导航栏时,我让它看起来像这样: 在此处输入图片说明 The only problem I have with coding this is that when I add the .png images for each icon into my XAML code on the MainPage.xaml the icons end up looking like this (the icons just turn to into black shapes with no other colours).我在编码时遇到的唯一问题是,当我将每个图标的 .png 图像添加到 MainPage.xaml 上的 XAML 代码中时,图标最终看起来像这样(图标只是变成黑色形状,没有其他颜色) . 在此处输入图片说明

Is android only capable of handling single colour icons for Android? android 是否只能处理 Android 的单色图标? Is there any way to disable this default feature?有什么办法可以禁用这个默认功能吗?

I know that the android style guidelines specify that you shouldn't use more than one colour for the icon in your navigation bar but I need to make an exception for this app.我知道 android 样式指南指定导航栏中的图标不应使用超过一种颜色,但我需要为此应用程序设置一个例外。

If you want to use the colorful icon in the Tabbedpage, you should create a custom renderer for Tabbedpage如果你想在 Tabbedpage 中使用彩色图标,你应该为 Tabbedpage 创建一个自定义渲染器

using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Widget;
using TabbedDemo.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace TabbedDemo.Droid
{
    public class MyTabbedRenderer : TabbedPageRenderer
    {
        public MyTabbedRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null && e.NewElement != null)
            {
                for (int i = 0; i <= this.ViewGroup.ChildCount - 1; i++)
                {
                    var childView = this.ViewGroup.GetChildAt(i);
                    if (childView is ViewGroup viewGroup)
                    {
                        for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
                        {
                            var childRelativeLayoutView = viewGroup.GetChildAt(j);
                            if (childRelativeLayoutView is BottomNavigationView)
                            {
                                ((BottomNavigationView)childRelativeLayoutView).ItemIconTintList = null;
                            }
                        }
                    }
                }
            }
        }
    }
}

Here is running screenshot.这是运行截图。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM