简体   繁体   English

Xamarin.Forms:如何更改 TabbedPage 中选项卡的 FontFamily?

[英]Xamarin.Forms: How to change FontFamily of tabs in TabbedPage?

How do I change the font style and size on tabs?如何更改选项卡上的字体样式和大小?

在此处输入图像描述 MainPage.xaml主页.xaml

<TabbedPage.Children>
    <local:Page1/>
    <local:Page2/>
    <local:Page3/>
</TabbedPage.Children>

Page1.xaml第1页.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="HelpMe.Page1"
             Title="Page 1">

Unfortunately, there is no direct cross platform way of achieving this in Xamarin.Forms不幸的是,在 Xamarin.Forms 中没有直接的跨平台方式来实现这一点

You have to write your own Effect or create a Custom renderer deriving from the native platform renderer.您必须编写自己的Effect器或创建从本机平台渲染器派生的自定义渲染器。 In your case it will be TabbedPageRenderer .在您的情况下,它将是TabbedPageRenderer Once you are able to register your custom renderer on the platform, you should be able to access the native Views on the platform and use platform specific APIs and set native properties as desired.一旦您能够在平台上注册自定义渲染器,您应该能够访问平台上的本机视图并使用特定于平台的 API 并根据需要设置本机属性。

Update:更新:

To help you achieve what I said above, here is the code for Android.为了帮助您实现我上面所说的,这里是 Android 的代码。 You would do something similar on iOS你会在 iOS 上做类似的事情

[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]

namespace YourNamespace.Renderers
{
    public class CustomTabbedPageRenderer : TabbedPageRenderer
    {
        public CustomTabbedPageRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (Element == null)
            {
                return;
            }

            UpdateTabLayout();

        }


        private void UpdateTabLayout()
        {
            if (!(GetChildAt(1) is TabLayout tabLayout)) return;

            var vg = (ViewGroup) tabLayout.GetChildAt(0);
            var tabsCount = vg.ChildCount;
            for (var i = 0; i < tabsCount; i++)
            {
                var vgTab = (ViewGroup) vg.GetChildAt(i);
                var tabChildCount = vgTab.ChildCount;
                for (var j = 0; j < tabChildCount; j++)
                {
                    var tabViewChild = vgTab.GetChildAt(j);
                    if (tabViewChild is TextView textView)
                    {
                        textView.SetTypeface(Typeface.SansSerif, TypefaceStyle.Italic);
                    }
                }
            }

        }
    }
}

The above code is trying to set the font as SansSerif with the Italic style and I have tested the code, it works as expected.上面的代码试图将字体设置为Italic样式的SansSerif ,我已经测试了代码,它按预期工作。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM