How can I change the background color of the Android tab with a renderer, while using a custom renderer to add some padding
In your SetAppearance
method, you can set the background color like this:
bottomView.SetBackgroundColor(Android.Graphics.Color.Red);
However, the SetBackgroundColor
requires a color of type Android.Graphics.Color
and I see that you wish to take the color from a DynamicResource, which will return a Xamarin.Forms.Color
. The good thing is that both color classes are easily convertable so we'll only need to take the current value of the DynamicResource:
if (Application.Current.Resources["CustomTabBackgroundColor"] is Xamarin.Forms.Color color)
{
bottomView.SetBackgroundColor(color.ToAndroid());
}
Here, CustomTabBackgroundColor
is the key for your color and color.ToAndroid()
does the convertion between the 2 classes (from Xamarin's to Android's one).
UPDATE
Due to an update to the question, I'll add that the issue with the styles/colors resetting isn't in this case due to the backround change, but it's due to something else entirely. The way that a new view appearance is being set (return new MarginedTabBarAppearance();
) resets all of the default styles. Since we are implementing everything from an interface and not deriving from the base class, we can't take anything from the already preset values. In order to fix that, we'll need to change the way the MarginedTabBarAppearance
class is being implemented:
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());
}
}
}
Also, you will have to change your CreateBottomNavViewAppearanceTracker
method like this:
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MarginedTabBarAppearance(this, shellItem);
}
This way we'll take everything that we have already styled and we'll simply change what we'll need.