Xamarin Android 中修改 TabbedPage 标题字体

写在前面,本人在 Stackoverflow 上面做 Xamarin 的技术支持也有一段时间了,最近都比较有空,就在这总结一下自己解决的许多问题,希望能帮到那些有限的,屈指可数的国内 Xamarin 开发者

我只负责解决 Xamarin.Android 这一块的问题

问题来源

https://stackoverflow.com/questions/46513240/xamarin-forms-change-the-tabbar-size

分析

TabbedPage 中没有办法直接修改字体及大小,必须要使用 Renderer,然后在 native 中去修改相应的属性,在 native Android 中 TabbedPage 会渲染成 TabLayout, 所以我们要研究 如何修改 TabbedPage 中的字体

TabLayout 中修改

https://stackoverflow.com/questions/30754203/tablayout-tab-style

Renderer

TabbedPage1

<?xml version="1.0" encoding="utf-8" ?>
<local:StyledTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FormsIssue01.TabbedPage1"
                        xmlns:local="clr-namespace:FormsIssue01"
            >
  <!--Pages can be added as references or inline-->
    <ContentPage Title="Over" />
    <ContentPage Title="FaceBook" />
    <ContentPage Title="Youtube" />
    <ContentPage Title="Contact" />
    <ContentPage Title="Contact2" />
    <ContentPage Title="Contact3" />
    
</local:StyledTabbedPage>

StyledTabbedPage

public class StyledTabbedPage : TabbedPage
{
}

StyledTabbedPageRenderer

public class StyledTabbedPageRenderer : TabbedPageRenderer
{
    private TabLayout tabLayout = null;

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

        this.tabLayout = (TabLayout)this.GetChildAt(1);

        //Method 2
        changeTabsFont();

        //Method 1
        tabLayout.TabMode = TabLayout.ModeScrollable;
        tabLayout.TabGravity = TabLayout.GravityFill;
    }

    private void changeTabsFont()
    {
        //Typeface font = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "fonts/" + Constants.FontStyle);
        ViewGroup vg = (ViewGroup)tabLayout.GetChildAt(0);
        int tabsCount = vg.ChildCount;
        for (int j = 0; j < tabsCount; j++)
        {
            ViewGroup vgTab = (ViewGroup)vg.GetChildAt(j);
            int tabChildsCount = vgTab.ChildCount;
            for (int i = 0; i < tabChildsCount; i++)
            {
                Android.Views.View tabViewChild = vgTab.GetChildAt(i);
                if (tabViewChild is TextView)
                {
                    //((TextView)tabViewChild).Typeface = font;
                    ((TextView)tabViewChild).TextSize = 6;
                }
            }
        }
    }
}

注意

Method 1 和 Method 2 不兼容,调整字体可以,但是不好看,推荐 Method 1

Update on 10/25/2018

TabbedPage 使用 android:TabbedPage.ToolbarPlacement="Bottom" 时上面的解决方案就不行了,参考下面链接

https://stackoverflow.com/questions/52942280/how-to-change-tabbar-text-size-when-using-tabbedpage-toolbarplacement-bottom-i

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值