温馨提示:本文翻译自stackoverflow.com,查看原文请点击:xamarin - Change TabbedPage Item FontSize on Android
android xamarin xamarin.forms font-size tabbedpage

xamarin - 在Android上更改TabbedPage项目FontSize

发布于 2020-03-29 13:12:55

在我的xamarin表单项目中,我将TabbedPage放在底部。

在Android上,字体大小太大。

我正在寻找一种减小字体大小的方法。

我还想消除使所选菜单项变大的影响。

<TabbedPage 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"
            xmlns:views="clr-namespace:namespace.Views"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            xmlns:i18n="clr-namespace:namespace.Utils;assembly=namespace"
            Title="{Binding Title}"
            android:TabbedPage.ToolbarPlacement="Bottom"
            android:TabbedPage.BarItemColor="#002244"
            android:TabbedPage.BarBackgroundColor="White"
            android:TabbedPage.BarSelectedItemColor="#096cd0"
            x:Class="namespace.Views.MainPage">
    <TabbedPage.Children>
        <NavigationPage x:Name="Home" Title="{i18n:Translate Menu_Home}" IconImageSource="accueil.png">
            <x:Arguments>
                <views:Home />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage x:Name="Services" Title="{i18n:Translate Menu_MyServices}" IconImageSource="services.png">
            <x:Arguments>
                <views:MyServices />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage x:Name="Documentation" Title="{i18n:Translate Menu_Documentation}" IconImageSource="documentation.png">
            <x:Arguments>
                <views:Documentation />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage x:Name="VideoCall" Title="{i18n:Translate Menu_Video}" IconImageSource="videoconferenc.png">
            <x:Arguments>
                <views:VideoCall />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

这是结果

  • 我们发现“视频咨询”没有足够的空间。

    Accueil

  • 这里更糟

    视频电话

  • 选择后,“服务”一词消失。

    服务

我已经做了很多研究,但是我找到了一种使之起作用的方法。

当菜单位于顶部时,我可以更改style.xml中的某些设置,但是当它位于底部时似乎不起作用。

你有解决方案吗?

非常感谢你,

克里斯

查看更多

查看更多

提问者
Christopher St-Pierre
被浏览
142
Jack Hua - MSFT 2020-01-31 17:26

你可以按照这个博客改变FontSizetabbedPageAndroid上的项目,写custom rendererTabbedPage和改变的textSize还有:

[assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace CustomTabbedPage.Droid
{
    public class ExtendedTabbedPageRenderer : TabbedPageRenderer
    {
        Xamarin.Forms.TabbedPage tabbedPage;
        BottomNavigationView bottomNavigationView;
        Android.Views.IMenuItem lastItemSelected;
        private bool firstTime = true;
        int lastItemId=-1;
        public ExtendedTabbedPageRenderer(Context context) : base(context)
        {
        }

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

            if (e.NewElement != null)
            {
                tabbedPage = e.NewElement as ExtendedTabbedPage;
                bottomNavigationView = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;


                //Call to change the font
                ChangeFont();
            }
        }

        //Change Tab font
        void ChangeFont()
        {
            var fontFace = Typeface.CreateFromAsset(Context.Assets, "gilsansultrabold.ttf");
            var bottomNavMenuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView;

            for (int i = 0; i < bottomNavMenuView.ChildCount; i++)
            {
                var item = bottomNavMenuView.GetChildAt(i) as BottomNavigationItemView;
                var itemTitle = item.GetChildAt(1);

                var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
                var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));

                lastItemId = bottomNavMenuView.SelectedItemId;

                //smallTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
                //largeTextView.SetTypeface(fontFace, TypefaceStyle.Bold);

                smallTextView.TextSize = 18;
                largeTextView.TextSize = 18;

                //Set text color
                var textColor = (item.Id == bottomNavMenuView.SelectedItemId) ? tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid() : tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
                smallTextView.SetTextColor(textColor);
                largeTextView.SetTextColor(textColor);
            }
        }      
    }
}

改变textSizesmallTextView,并largeTextView会工作。您也可以在此处下载示例项目