In my application I set the bottom tab bar background color like this:
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="M.Views.AppShell"
NavBarIsVisible="True"
xmlns:local="clr-namespace:Memorise"
BackgroundColor="{DynamicResource ShellBackgroundColor}"
TabBarBackgroundColor="{DynamicResource TabBarBackgroundColor}"
TabBarDisabledColor="{DynamicResource TabBarDisabledColor}"
TabBarForegroundColor="{DynamicResource TabBarForegroundColor}"
TabBarTitleColor="{DynamicResource TabBarTitleColor}"
TabBarUnselectedColor="{DynamicResource TabBarUnselectedColor}"
I create a custom renderer to add some side padding to the tabs on Android:
public class CustomShellRenderer : ShellRenderer
{
public CustomShellRenderer(Context context)
: base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MarginedTabBarAppearance();
}
}
public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
if (App.devWidth == SIZE.L)
bottomView.SetPadding(400, 0, 400, 0);
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
}
Unfortunately this also has a side effect in that it sets the background color of the tab to white.
Does anyone have any knowledge of how I use this renderer and also change the background color to be the same as the DynamicResource ShellBackgroundColor?
Code with renderers commented out:
public class CustomShellRenderer : ShellRenderer
{
public CustomShellRenderer(Context context)
: base(context)
{
}
//protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
//{
// return new MarginedTabBarAppearance();
//}
}
public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
//if (App.devWidth == SIZE.L)
// bottomView.SetPadding(400, 0, 400, 0);
//if (Application.Current.Resources["TabBarBackgroundColor"] is Xamarin.Forms.Color color1)
//{
// bottomView.SetBackgroundColor(color1.ToAndroid());
//}
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
}
Tab bars with renderers commented out:
** Code with renderers not commented out:**
public class CustomShellRenderer : ShellRenderer
{
public CustomShellRenderer(Context context)
: base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MarginedTabBarAppearance();
}
}
public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
if (App.devWidth == SIZE.L)
bottomView.SetPadding(400, 0, 400, 0);
if (Application.Current.Resources["TabBarBackgroundColor"] is Xamarin.Forms.Color color1)
{
bottomView.SetBackgroundColor(color1.ToAndroid());
}
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
}
Tab bars with renderers not commented out:
Updated code suggested by Mihail:
public class MarginedTabBarAppearance : ShellBottomNavViewAppearanceTracker
{
public MarginedTabBarAppearance(IShellContext shellContext, ShellItem shellItem)
: base(shellContext, shellItem)
{
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
base.SetAppearance(bottomView, appearance);
bottomView.SetPadding(400, 0, 400, 0);
if (Application.Current.Resources.ContainsKey("TabBarBackgroundColor") &&
Application.Current.Resources["TabBarBackgroundColor"] is Color tabColor)
{
bottomView.SetBackgroundColor(tabColor.ToAndroid());
}
}
}



