在我的应用程序中,我设置底部标签栏背景颜色,如下所示:
<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}"
我创建了一个自定义渲染器来向 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)
{
}
}
不幸的是,这也有一个副作用,它将选项卡的背景颜色设置为白色。
有谁知道我如何使用此渲染器并将背景颜色更改为与 DynamicResource ShellBackgroundColor 相同?
带有渲染器的代码被注释掉:
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)
{
}
}
带有注释掉的渲染器的选项卡栏:
** 带有渲染器的代码未注释掉:**
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)
{
}
}
带有渲染器的选项卡栏未注释掉:
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());
}
}
}