Hello,
You can do this by creating handler for tabbedpage, then set TypeFace for every tab.
Firstly, open your Platforms/Android/Resources
folder, then create a folder called raw
, then copy your font in it and make sure the font file's build action is AndroidResource.
By the way, I make the font file's name to lower case and delete other characters like _ or -
.
Then you can create MyTabbedViewHandler.cs in Platforms/Android/
folder. I access the TabLayout and get every tab by var child = view1?.GetChildAt(j);
, then set tab text font by tv.Typeface = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.GetFont(Resource.Raw.alexbrushregular);
using Android.Widget;
using Google.Android.Material.Tabs;
using Microsoft.Maui.Handlers;
using System.Reflection;
namespace YourProject.Platforms.Android
{
public class MyTabbedViewHandler : TabbedViewHandler
{
public override void SetVirtualView(IView view)
{
base.SetVirtualView(view);
SetTabFont(view);
}
private bool SetTabFont(IView view)
{
try
{
FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(view);
FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_tabLayout", BindingFlags.NonPublic | BindingFlags.Instance);
TabLayout tabLayout = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as TabLayout;
if (tabLayout != null)
{
for (int i = 0; i < tabLayout.TabCount; i++)
{
TabLayout.Tab? tab = tabLayout.GetTabAt(i);
TabLayout.TabView? view1 = tab?.View;
for (int j = 0; j < (view1?.ChildCount ?? 0); j++)
{
var child = view1?.GetChildAt(j);
if (child is TextView tv)
{
tv.Typeface = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.GetFont(Resource.Raw.alexbrushregular);
}
}
}
return true;
}
return false;
}
catch
{
return false;
}
}
}
}
In the end, you need to add this handler by builder in MauiProgram.cs.
builder
.ConfigureMauiHandlers(handlers => {
#if ANDROID
// Add a handler
handlers.AddHandler(typeof(TabbedPage), typeof(YourProject.Platforms.Android.MyTabbedViewHandler));
#endif
});
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.