示例#1
0
        public static void UpdateBarTextColor(this AToolbar nativeToolbar, Toolbar toolbar)
        {
            var textColor = toolbar.BarTextColor;

            // Because we use the same toolbar across multiple navigation pages (think tabbed page with nested NavigationPage)
            // We need to reset the toolbar text color to the default color when it's unset
            if (_defaultTitleTextColor == null)
            {
                var a = TintTypedArray.ObtainStyledAttributes(nativeToolbar.Context?.GetThemedContext(), null, Resource.Styleable.Toolbar, Resource.Attribute.toolbarStyle, 0);
                _defaultTitleTextColor = a.GetColorStateList(Resource.Styleable.Toolbar_titleTextColor);
                a.Recycle();
            }

            if (textColor != null)
            {
                nativeToolbar.SetTitleTextColor(textColor.ToPlatform().ToArgb());
            }
            else
            {
                nativeToolbar.SetTitleTextColor(_defaultTitleTextColor);
            }

            if (nativeToolbar.NavigationIcon is DrawerArrowDrawable icon)
            {
                if (textColor != null)
                {
                    _defaultNavigationIconColor = icon.Color;
                    icon.Color = textColor.ToPlatform().ToArgb();
                }
                else if (_defaultNavigationIconColor != null)
                {
                    icon.Color = _defaultNavigationIconColor.Value;
                }
            }
        }
        protected virtual void SetColors(AToolbar toolbar, IShellToolbarTracker toolbarTracker, Color foreground, Color background, Color title)
        {
            var titleArgb = title.ToAndroid(ShellRenderer.DefaultTitleColor).ToArgb();

            if (_titleTextColor != titleArgb)
            {
                toolbar.SetTitleTextColor(titleArgb);
                _titleTextColor = titleArgb;
            }

            var newColor = background.ToAndroid(ShellRenderer.DefaultBackgroundColor);

            if (!(toolbar.Background is ColorDrawable cd) || cd.Color != newColor)
            {
                using (var colorDrawable = new ColorDrawable(background.ToAndroid(ShellRenderer.DefaultBackgroundColor)))
                    toolbar.SetBackground(colorDrawable);
            }

            var newTintColor = foreground ?? ShellRenderer.DefaultForegroundColor;

            if (toolbarTracker.TintColor != newTintColor)
            {
                toolbarTracker.TintColor = newTintColor;
            }
        }