【问题标题】:Change TabbedPage Item FontSize on Android在 Android 上更改 TabbedPage 项字体大小
【发布时间】:2020-05-16 11:11:19
【问题描述】:

我在我的 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>

这是结果

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

  • 这里更糟

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

我已经做了很多研究,但我找到了一种让它发挥作用的方法。

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

你有解决办法吗?

非常感谢,

克里斯

【问题讨论】:

  • 另外,如果是字体大小的问题,您可以阅读xaml中使用的文档/示例,然后您可以专门为android设置字体值

标签: android xamarin xamarin.forms font-size tabbedpage


【解决方案1】:

您可以在Android上关注this blog更改tabbedPageFontSize项目,写一个TabbedPagecustom renderer并在那里更改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);
            }
        }      
    }
}

更改smallTextViewlargeTextViewtextSize 将起作用。您也可以下载示例项目here

【讨论】:

  • 您知道如何在 xamarin ios 中将 TabbedPage 的标题拆分为 2 行(多行)吗?我知道 contenpage 中多行标题的解决方案。但是当我使用多行标题内容页面作为 TabbedPage 的选项卡时,第二行标题不可见
  • @Blu 我不知道。但我想这是不可能的,因为第二行没有足够的空间。
  • 间距没问题,我的标签页没有图标,我什至可以将填充设置为从底部开始的标题文本,以便文本有足够的空间用于多行。
【解决方案2】:

要更改底部标签页的字体大小,请参阅下面的链接...它适用于我。只需按照以下步骤创建自定义渲染器,而不是创建自定义渲染器。

1) 在 Android -> Resources/Values 中创建新文件“dimens.xml”(仅当它不退出时) 2)然后复制以下链接中的代码

链接:

https://montemagno.com/control-text-size-on-android-bottom-navigation/

【讨论】:

  • 请将整个代码改为链接到其他网站。这在外部网站无法访问时很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多