【问题标题】:How to change TabBar font family when using TabbedPage.ToolbarPlacement=“Bottom” in Xamarin Android?在 Xamarin Android 中使用 TabbedPage.ToolbarPlacement=“Bottom”时如何更改 TabBar 字体系列?
【发布时间】:2021-03-02 22:29:05
【问题描述】:

我在 NavigationBar.xaml 中将这段代码用于我的 tabbedPage

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:xyz" 
        x:Class="xyz.NavigationBar"
            android:TabbedPage.ToolbarPlacement="Bottom"
        BarBackgroundColor="#000000"
        android:TabbedPage.BarItemColor="#FFFFFF"
        android:TabbedPage.BarSelectedItemColor="Red"
        >
</TabbedPage>

工具栏放置在底部。

感谢 James Montemagno 的一篇文章,我已经能够更改文本大小:

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

他在其中指出,可以通过在 Resources/values 中添加一个 dimen.xml 文件来更改文本大小,并使用以下代码:

<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>
    <dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
</resources>

这很好。 但是,是否可以使用相同的方法更改字体系列?我也在使用自定义字体。

提前致谢

【问题讨论】:

  • 有人知道怎么做吗?

标签: xml xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

创建一个新的font 文件夹并将.tff 文件和一个名为myfont 的新xml 文件添加到其中。

myfont.xml

<?xml version="1.0" encoding="utf-8" ?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
   <font android:font="@font/a"
      android:fontStyle="normal"
      android:fontWeight="400"
   />
</font-family>

然后在您的Resources/values/styles 文件中添加底部导航视图的自定义样式。

<style name="MyBottomNavigationView"
        parent="Widget.Design.BottomNavigationView">
   <item name="fontFamily">@font/myfont</item>
</style>

终于在你的自定义标签页渲染器中:

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedPageRenderer))]
namespace EntryCa.Droid
{
  class MyTabbedPageRenderer:TabbedPageRenderer
  {
      private Context _context;
      public MyTabbedPageRenderer(Context context):base(context)
      {
         _context = context;
      }

      protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
      {
          base.OnElementChanged(e);
          if (e.OldElement == null && e.NewElement != null)
          {
              for (int i = 0; i <= this.ViewGroup.ChildCount - 1; i++)
              {
                var childView = this.ViewGroup.GetChildAt(i);
                if (childView is ViewGroup viewGroup)
                {
                    for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
                    {
                        var childRelativeLayoutView = viewGroup.GetChildAt(j);
                        if (childRelativeLayoutView is BottomNavigationView)
                        {
                            ((BottomNavigationView)childRelativeLayoutView).ItemTextAppearanceActive = Resource.Style.MyBottomNavigationView;
                            ((BottomNavigationView)childRelativeLayoutView).ItemTextAppearanceInactive = Resource.Style.MyBottomNavigationView;
                        }
                    }
                }
            }
         }
      }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 2021-06-07
    • 1970-01-01
    • 2019-11-08
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多