Xamarin.Forms安卓端Tab标签字体修改问题:fontFamily属性无效
解决Xamarin.Forms Android中TabLayout自定义字体不生效的问题
我之前也碰到过这个问题,Xamarin.Forms的TabBar在Android端确实不会直接响应fontFamily属性,得用点小技巧来搞定。下面给你两个靠谱的解决方案:
方案一:自定义TabbedPage渲染器(推荐,灵活可控)
这是最通用的方法,能统一设置所有Tab的字体,不用逐个修改XML。
先把你的自定义字体文件(比如
MyFont.ttf)放到安卓项目的Assets文件夹里,右键字体文件,设置生成操作为AndroidAsset。在安卓项目中创建一个自定义渲染器类:
using Android.Content; using Android.Support.Design.Widget; using Android.Widget; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using YourAppNamespace.Droid; [assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))] namespace YourAppNamespace.Droid { public class CustomTabbedPageRenderer : TabbedPageRenderer { public CustomTabbedPageRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) { base.OnElementChanged(e); if (ViewGroup != null && ViewGroup.ChildCount > 0) { var tabLayout = ViewGroup.GetChildAt(1) as TabLayout; if (tabLayout != null) { for (int i = 0; i < tabLayout.TabCount; i++) { var tab = tabLayout.GetTabAt(i); if (tab?.CustomView != null) { // 如果Tab有自定义View,找到对应的TextView var tabTextView = tab.CustomView.FindViewById<TextView>(Resource.Id.tab_label); tabTextView?.SetTypeface(Android.Graphics.Typeface.CreateFromAsset(Context.Assets, "MyFont.ttf"), Android.Graphics.TypefaceStyle.Normal); } else { // 直接获取TabLayout默认的TextView var tabTextView = (TextView)tabLayout.GetChildAt(i); tabTextView.SetTypeface(Android.Graphics.Typeface.CreateFromAsset(Context.Assets, "MyFont.ttf"), Android.Graphics.TypefaceStyle.Normal); } } } } } } }
- 记得把
YourAppNamespace替换成你实际的应用命名空间,MyFont.ttf换成你的字体文件名(要和Assets里的完全一致,包括大小写)。
方案二:修改Tabbar.xml,用自定义Tab View设置字体
如果你不想写渲染器,也可以直接在XML里为每个Tab指定带自定义字体的TextView:
在安卓项目的
Resources目录下新建font文件夹,把字体文件放进去,设置生成操作为AndroidResource(文件名建议小写,比如myfont.ttf)。修改
Resources/layout/Tabbar.xml,为每个TabItem添加自定义TextView:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!-- 第一个Tab --> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tab_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textSize="14sp" android:fontFamily="@font/myfont" /> <!-- 引用font目录下的字体 --> </android.support.design.widget.TabItem> <!-- 第二个Tab --> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tab_profile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的" android:textSize="14sp" android:fontFamily="@font/myfont" /> </android.support.design.widget.TabItem> </android.support.design.widget.TabLayout>
几个要注意的坑
- 字体文件名不要有空格、特殊字符,否则会加载失败。
- 一定要确认字体文件的生成操作设置正确,这是最容易踩的坑!
- 如果用渲染器,要确保没有其他自定义TabbedPage渲染器和它冲突。
内容的提问来源于stack exchange,提问作者Grounch
相关产品推荐
相关产品推荐

