android - XAMARIN:跨平台 FontFamily

标签 android fonts xamarin

我需要为我的应用程序中的不同标签指定不同的 FontFamily。我需要使用默认字体(例如 Android 的 Roboto 和 iOS 的 Helvetica)及其修改(例如 Light、Medium、Bold)。据我所知,我应该使用 Roboto-Light 和 Helvetica-Light 来获得字体的 Light 版本(Medium 和 Bold 也一样)。 除了这个要求之外,我还需要在 XAML 中设置字体(比如 described in documentation )所以我最终得到了这段代码

<StackLayout BackgroundColor="#F8F8F8" Padding="0, 20, 0, 0">
<Label Text="Black" TextColor="#000000" >
  <Label.FontSize>
    <OnPlatform x:TypeArguments="x:Double"
                iOS="17"
                Android="16"
                WinPhone="16" />
  </Label.FontSize>
  <Label.FontFamily>
    <OnPlatform x:TypeArguments="x:String">
      <OnPlatform.iOS>Helvetica-Black</OnPlatform.iOS>
      <OnPlatform.Android>Roboto-Black</OnPlatform.Android>
      <OnPlatform.WinPhone></OnPlatform.WinPhone>
    </OnPlatform>
  </Label.FontFamily>
</Label>
<Label Text="Light" TextColor="#000000">
  <Label.FontSize>
    <OnPlatform x:TypeArguments="x:Double"
                iOS="17"
                Android="16"
                WinPhone="16" />
  </Label.FontSize>
  <Label.FontFamily>
    <OnPlatform x:TypeArguments="x:String">
      <OnPlatform.iOS>Helvetica-Light</OnPlatform.iOS>
      <OnPlatform.Android>Roboto-Light</OnPlatform.Android>
      <OnPlatform.WinPhone></OnPlatform.WinPhone>
    </OnPlatform>
  </Label.FontFamily>
</Label>
<Label Text="Medium" TextColor="#000000" >
  <Label.FontSize>
    <OnPlatform x:TypeArguments="x:Double"
                iOS="17"
                Android="16"
                WinPhone="16" />
  </Label.FontSize>
  <Label.FontFamily>
    <OnPlatform x:TypeArguments="x:String">
      <OnPlatform.iOS>Helvetica-Medium</OnPlatform.iOS>
      <OnPlatform.Android>Roboto-Medium</OnPlatform.Android>
      <OnPlatform.WinPhone></OnPlatform.WinPhone>
    </OnPlatform>
  </Label.FontFamily>
</Label>
<Label Text="Bold"  TextColor="#000000">
  <Label.FontSize>
    <OnPlatform x:TypeArguments="x:Double"
                iOS="17"
                Android="16"
                WinPhone="16" />
  </Label.FontSize>
  <Label.FontFamily>
    <OnPlatform x:TypeArguments="x:String">
      <OnPlatform.iOS>Helvetica-Bold</OnPlatform.iOS>
      <OnPlatform.Android>Roboto-Bold</OnPlatform.Android>
      <OnPlatform.WinPhone></OnPlatform.WinPhone>
    </OnPlatform>
  </Label.FontFamily>
</Label>

然而,Android 中的结果却出人意料。不同 Label 的 FontFamily 没有变化。它们看起来都一样。

Android

iOS 中的相同代码按预期工作

enter image description here

我的问题是:如何在我的 Android 应用程序中获取 Roboto-LightRoboto-MediumRoboto-Bold 字体,如果遵循XAMARIN 文档不起作用?

最佳答案

更新:

我没有看到您第一次使用 API 18/4.3(在您的模拟器的标题栏中),以为您将它们加载为旧 android 版本的自定义 Assets 。由于 roboto 是自 4.1 以来的默认字体,您可以将它们用作:

  • 无衬线字体
  • 无衬线字体
  • 无衬线字体
  • sans-serif-thin (4.2+)

原文:

https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/

Xamarin.Forms for Android does not currently expose the ability to set the font to a custom font file, so custom renderers are required. When writing the renderer for the Android platform, create a Typeface instance that references a custom font file that has been added to the Assets directory of the application (with Build Action: AndroidAsset).

[assembly: ExportRenderer (typeof (MyLabel), typeof (MyLabelRenderer))]
namespace WorkingWithFonts.Android {
    public class MyLabelRenderer : LabelRenderer {
        protected override void OnElementChanged (ElementChangedEventArgs<Label> e) {
            base.OnElementChanged (e);
            var label = (TextView)Control; // for example
            Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "SF Hollywood Hills.ttf");
            label.Typeface = font;
        }
    }
}

关于android - XAMARIN:跨平台 FontFamily,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732381/

相关文章:

java - 获取点击图像属性

java - 内存不足(堆)问题无法修复

vue.js - Nuxt 2.15.7 构建错误 - 无法解析 CSS @font-face URL

ios - 具有动态单元格高度的 UITableViewCell 内的 UITableView

android - Android 中的 Facebook 推送通知集成

css - 如何用css叠加两种字体

css - 将自定义字体添加到 laravel 应用程序

ios - 使用 MVVMCross 在 iOS 上实现搜索

android - 如何在 Xamarin 中获取 ActivityManager 的实例?

android - 使用 LinearLayout 将按钮底部对齐