Xamarin Forms:如何更改 Android 的导航标题字体系列?
Xamarin Forms: How to Change Navigation Title Font Family for Android?
我正在使用 AppShell 和 Flyout 开发 Xamarin Forms 项目。我想更改导航标题的字体系列。我试图找到方法来做到这一点,但不幸的是我无法解决这个问题。我对在线解决方案的尝试之一是使用下面的代码使用自定义渲染。它警告我 NavigationPageRenderer
已过时,当我 运行 它时,我得到一个异常。请帮我想办法更改导航标题字体系列。
自定义渲染:
[assembly: ExportRenderer(typeof(AppShell), typeof(CustomNavigationPageRenderer))]
namespace MyApp.Droid.CustomRenderers
{
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
private Android.Support.V7.Widget.Toolbar _toolbar;
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
{
_toolbar = (Android.Support.V7.Widget.Toolbar)child;
_toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
{
var view = e.Child.GetType();
if (e.Child.GetType() == typeof(Android.Widget.TextView))
{
var textView = (Android.Widget.TextView)e.Child;
var spaceFont = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, "Montserrat-SemiBold.ttf");
textView.Typeface = spaceFont;
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
}
}
警告:
'NavigationPageRenderer.NavigationPageRenderer()' is obsolete: 'This constructor is obsolete as of version 2.5. Please use NavigationPageRenderer(Context) instead.
抛出异常:
(在 'MainActivity' Class 中的 'LoadApplication(new App())'")
System.ArgumentException: 'element is not of type Xamarin.Forms.NavigationPage
Parameter name: element'
你能试试这个吗?在您要更改标题字体的页面中,添加自定义字体后,将其添加到 xaml。将 FontFamily 更改为您想要的字体。
<NavigationPage.TitleView>
<StackLayout>
<Label FontFamily=""/>
</StackLayout>
</NavigationPage.TitleView>
我正在使用 AppShell 和 Flyout 开发 Xamarin Forms 项目。我想更改导航标题的字体系列。我试图找到方法来做到这一点,但不幸的是我无法解决这个问题。我对在线解决方案的尝试之一是使用下面的代码使用自定义渲染。它警告我 NavigationPageRenderer
已过时,当我 运行 它时,我得到一个异常。请帮我想办法更改导航标题字体系列。
自定义渲染:
[assembly: ExportRenderer(typeof(AppShell), typeof(CustomNavigationPageRenderer))]
namespace MyApp.Droid.CustomRenderers
{
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
private Android.Support.V7.Widget.Toolbar _toolbar;
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
{
_toolbar = (Android.Support.V7.Widget.Toolbar)child;
_toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
{
var view = e.Child.GetType();
if (e.Child.GetType() == typeof(Android.Widget.TextView))
{
var textView = (Android.Widget.TextView)e.Child;
var spaceFont = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, "Montserrat-SemiBold.ttf");
textView.Typeface = spaceFont;
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
}
}
警告:
'NavigationPageRenderer.NavigationPageRenderer()' is obsolete: 'This constructor is obsolete as of version 2.5. Please use NavigationPageRenderer(Context) instead.
抛出异常: (在 'MainActivity' Class 中的 'LoadApplication(new App())'")
System.ArgumentException: 'element is not of type Xamarin.Forms.NavigationPage
Parameter name: element'
你能试试这个吗?在您要更改标题字体的页面中,添加自定义字体后,将其添加到 xaml。将 FontFamily 更改为您想要的字体。
<NavigationPage.TitleView>
<StackLayout>
<Label FontFamily=""/>
</StackLayout>
</NavigationPage.TitleView>