protected virtual void SetColors(TabLayout tabLayout, Color foreground, Color background, Color title, Color unselected) { var titleArgb = title.ToPlatform(ShellRenderer.DefaultTitleColor).ToArgb(); var unselectedArgb = unselected.ToPlatform(ShellRenderer.DefaultUnselectedColor).ToArgb(); tabLayout.SetTabTextColors(unselectedArgb, titleArgb); tabLayout.SetBackground(new ColorDrawable(background.ToPlatform(ShellRenderer.DefaultBackgroundColor))); tabLayout.SetSelectedTabIndicatorColor(foreground.ToPlatform(ShellRenderer.DefaultForegroundColor)); }
void UpdateBarBackgroundColor() { if (_disposed || _tabLayout == null) { return; } Color tintColor = Element.BarBackgroundColor; if (Forms.IsLollipopOrNewer) { if (tintColor.IsDefault) { _tabLayout.BackgroundTintMode = null; } else { _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src; _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); } } else { if (tintColor.IsDefault && _backgroundDrawable != null) { _tabLayout.SetBackground(_backgroundDrawable); } else if (!tintColor.IsDefault) { if (_backgroundDrawable == null) { _backgroundDrawable = _tabLayout.Background; } _tabLayout.SetBackgroundColor(tintColor.ToAndroid()); } } }
void UpdateBarBackgroundColor() { if (IsDisposed) { return; } if (IsBottomTabPlacement) { Color tintColor = Element.BarBackgroundColor; if (tintColor.IsDefault) { _bottomNavigationView.SetBackground(null); } else if (!tintColor.IsDefault) { _bottomNavigationView.SetBackgroundColor(tintColor.ToAndroid()); } } else { Color tintColor = Element.BarBackgroundColor; if (Forms.IsLollipopOrNewer) { if (tintColor.IsDefault) { _tabLayout.BackgroundTintMode = null; } else { _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src; _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); } } else { if (tintColor.IsDefault && _backgroundDrawable != null) { _tabLayout.SetBackground(_backgroundDrawable); } else if (!tintColor.IsDefault) { // if you don't create a new drawable then SetBackgroundColor // just sets the color on the background drawable that's saved // it doesn't create a new one if (_backgroundDrawable == null && _tabLayout.Background != null) { _backgroundDrawable = _tabLayout.Background; _wrappedBackgroundDrawable = ADrawableCompat.Wrap(_tabLayout.Background).Mutate(); } if (_wrappedBackgroundDrawable != null) { _tabLayout.Background = _wrappedBackgroundDrawable; } _tabLayout.SetBackgroundColor(tintColor.ToAndroid()); } } } }
protected override void OnElementChanged(ElementChangedEventArgs <TabbedPage> e) { base.OnElementChanged(e); if (e.NewElement == null) { return; } SetGradientBackground(); var relativeLayout = this.GetChildAt(0) as Android.Widget.RelativeLayout; if (relativeLayout == null) { tabLayout = FindViewById <TabLayout>(Resource.Id.sliding_tabs); tabLayout.SetBackground(gradientDrawable); } else { ColorStateList iconsColorStates = new ColorStateList( new int[][] { new int[] { -Android.Resource.Attribute.StateChecked }, new int[] { Android.Resource.Attribute.StateChecked } }, new int[] { Android.Graphics.Color.AliceBlue, Android.Graphics.Color.White }); ColorStateList textColorStates = new ColorStateList( new int[][] { new int[] { -Android.Resource.Attribute.StateChecked }, new int[] { Android.Resource.Attribute.StateChecked } }, new int[] { Android.Graphics.Color.AliceBlue, Android.Graphics.Color.White }); bottomNavigationView = relativeLayout.GetChildAt(1) as BottomNavigationView; // --> Individually resize Tab bar icons on BottomNavigationView: //var bottomNavigationMenuView = (BottomNavigationMenuView)bottomNavigationView.GetChildAt(0); //var scale = Resources.DisplayMetrics.Density; //var paddingDp = 4; //var dpAsPixels = (int)(paddingDp * scale + 0.5f); //for (int i = 0; i < bottomNavigationMenuView.ChildCount; i++) //{ // var childView = bottomNavigationMenuView.GetChildAt(i); // var iconView = bottomNavigationMenuView.GetChildAt(i).FindViewById(Resource.Id.icon); // var layoutParams = iconView.LayoutParameters; // var displayMetrics = Resources.DisplayMetrics; // // --> If it is special menu item change the size, otherwise take other size: // if (i == 1) // { // // --> Set Padding individually: // //if (childView is BottomNavigationItemView tab) // //{ // // // --> Set Padding here: // // tab.SetPadding(tab.PaddingLeft, dpAsPixels, tab.PaddingRight, tab.PaddingBottom); // //} // // --> Set height here: // layoutParams.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 32, displayMetrics); // // --> Set width here: // layoutParams.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 48, displayMetrics); // } // else if (i == 2) // { // // --> Set Padding individually: // //if (childView is BottomNavigationItemView tab) // //{ // // // --> Set Padding here: // // tab.SetPadding(tab.PaddingLeft, dpAsPixels, tab.PaddingRight, tab.PaddingBottom); // //} // // --> Set height here: // layoutParams.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 24, displayMetrics); // // --> Set width here: // layoutParams.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 24, displayMetrics); // } // else // { // // --> Set Padding individually: // //if (childView is BottomNavigationItemView tab) // //{ // // // --> Set Padding here: // // tab.SetPadding(tab.PaddingLeft, dpAsPixels, tab.PaddingRight, tab.PaddingBottom); // //} // // --> Set height here: // layoutParams.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 28, displayMetrics); // // --> Set width here: // layoutParams.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 28, displayMetrics); // } // iconView.LayoutParameters = layoutParams; //} // --> Resize all Tab bar icons equally on BottomNavigationView: //bottomNavigationView.ItemIconSize = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 80, Resources.DisplayMetrics); bottomNavigationView.ItemIconTintList = iconsColorStates; bottomNavigationView.ItemTextColor = textColorStates; // --> Set Padding for all icons equally: bottomNavigationView.SetPadding(0, 15, 0, 0); bottomNavigationView.SetBackground(gradientDrawable); bottomNavigationView.Elevation = 0; } }