You need to enable JavaScript to run this app. Xamarin Forms Shell TabBar Android平台如何应用自定义Ubuntu字体
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms Shell TabBar Android平台如何应用自定义Ubuntu字体

Xamarin Forms Shell Android端TabBar自定义字体失效解决方案

你之前Android端自定义TabBar字体的代码不生效,核心是两个关键错误:

  • 字体加载路径不对:通过ExportFont导出的.NET Standard嵌入字体,不会自动同步到Android原生项目的Assets目录,Typeface.CreateFromAsset读取Assets路径的文件根本找不到目标字体,拿到的Typeface实例无效,自然无法生效。
  • 用错了外观控制入口:Android端Shell的底部TabBar基于BottomNavigationView实现,对应的外观自定义入口是IShellBottomNavViewAppearanceTracker,和iOS端用来控制UITabBar的IShellTabBarAppearanceTracker不是同一套逻辑,把修改代码放在iOS对应的接口实现里,Android端根本不会执行。

正确实现步骤

1. 实现可生效的自定义字体Span

Android原生的TypefaceSpan无法正确应用自定义Typeface的测量和绘制逻辑,需要自行实现Span类,确保字体替换真正生效:

public class CustomTypefaceSpan : MetricAffectingSpan
{
    private readonly Typeface _customTypeface;

    public CustomTypefaceSpan(Typeface customTypeface)
    {
        _customTypeface = customTypeface;
    }

    public override void UpdateMeasureState(TextPaint paint)
    {
        ApplyCustomTypeface(paint);
    }

    public override void UpdateDrawState(TextPaint paint)
    {
        ApplyCustomTypeface(paint);
    }

    private void ApplyCustomTypeface(TextPaint paint)
    {
        // 保留原文字的字重样式
        var originalStyle = paint.Typeface?.FontStyle ?? TypefaceStyle.Normal;
        paint.Typeface = Typeface.Create(_customTypeface, originalStyle);
        paint.FakeBoldText = originalStyle == TypefaceStyle.Bold;
    }
}

2. 编写Android端TabBar外观自定义Tracker

实现IShellBottomNavViewAppearanceTracker接口,借助Xamarin Forms内置的字体加载逻辑,直接获取已经通过ExportFont注册的自定义字体,不需要手动查找Assets路径:

public class CustomShellBottomNavViewAppearanceTracker : IShellBottomNavViewAppearanceTracker
{
    public void Dispose()
    {
    }

    public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
    {
        // 直接通过ExportFont注册的别名加载嵌入字体,自动匹配资源
        var ubuntuTypeface = Font.OfSize("UbuntuRegular", 10).ToTypeface();
        
        // 遍历所有底部Tab菜单项,逐个应用自定义字体
        for (int i = 0; i < bottomView.Menu.Size(); i++)
        {
            var tabMenuItem = bottomView.Menu.GetItem(i);
            var titleSpan = new SpannableString(tabMenuItem.Title ?? string.Empty);
            titleSpan.SetSpan(
                new CustomTypefaceSpan(ubuntuTypeface), 
                0, 
                titleSpan.Length, 
                SpanTypes.InclusiveInclusive
            );
            tabMenuItem.SetTitle(titleSpan);
        }
    }

    public void ResetAppearance(BottomNavigationView bottomView)
    {
    }
}

3. 注入自定义外观逻辑

在Android原生项目中自定义ShellRenderer,重写创建底部导航外观Tracker的方法,替换系统默认的实现:

[assembly: ExportRenderer(typeof(AppShell), typeof(CustomShellRenderer))]
namespace YourApp.Droid.Renderers
{
    public class CustomShellRenderer : ShellRenderer
    {
        protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
        {
            return new CustomShellBottomNavViewAppearanceTracker();
        }
    }
}

常见注意事项

  • 不需要手动将字体文件复制到Android项目的Assets目录,Font.ToTypeface()扩展方法会自动识别所有通过ExportFont注册的嵌入字体,避免重复维护多份字体资源。
  • 不要在View初始化的过早生命周期节点修改TabBar字体,此时BottomNavigationView的Menu还未完成数据加载,遍历菜单项会拿不到实际Tab内容,修改的样式也会被后续的默认初始化逻辑覆盖。
  • 如果需要给选中Tab应用粗体样式,只要替换代码中加载的字体别名为UbuntuBold,逻辑完全一致。

内容的提问来源于stack exchange,提问作者ArthurCPPCLI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.26 06:45:07