您的位置:首页 > 移动开发 > Android开发

Xamarin Android 中修改 TabbedPage 标题字体

2017-10-03 09:44 525 查看
写在前面,本人在 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐