Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin Java.Lang.RuntimeException:找不到字体资源Roboto Light(初学者)_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin Java.Lang.RuntimeException:找不到字体资源Roboto Light(初学者)

Xamarin Java.Lang.RuntimeException:找不到字体资源Roboto Light(初学者),xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有以下XAML代码: <Label Text="Hello Forms with XAML"> <Label.FontFamily> <OnPlatform x:TypeArguments="x:String"> <OnPlatform.iOS>Roboto-Light</OnPlatform.iOS> <OnPlatform

我有以下XAML代码:

<Label Text="Hello Forms with XAML">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String">
                <OnPlatform.iOS>Roboto-Light</OnPlatform.iOS>
                <OnPlatform.Android>Roboto-Light.ttf#Roboto-Light</OnPlatform.Android>
                <OnPlatform.WinPhone>Assets/Fonts/Roboto-Light.ttf#Roboto-Light</OnPlatform.WinPhone>
            </OnPlatform>
        </Label.FontFamily>
    </Label>
但是,我得到了错误Java.Lang.RuntimeException:Font asset not found Roboto Light,即使该字体位于Assets文件夹中:


如何解决此问题?

在Android上,您必须确保后面的部分是字体的实际名称。此部分与文件名不直接相同。我还在我的一个应用程序中使用Roboto,声明如下:

<OnPlatform.Android>fonts/Roboto-Regular.ttf#Roboto</OnPlatform.Android>
<OnPlatform.Android>fonts/Roboto-Bold.ttf#Roboto Bold</OnPlatform.Android>
您可能希望尝试以下方式:

<OnPlatform.Android>Roboto-Light.ttf#Roboto Light</OnPlatform.Android>
通过在FontBook中选择字体并检查其全名属性,可以在Mac上找到此名称


您可以在下面的网站上找到使用Xamarin.Forms中自定义字体的全面指南

正如Steven所强调的,确保字体ttf文件位于正确的位置非常重要,在这种情况下,您似乎做对了,android项目的位置实际上是“资产”文件夹

Android的表单可以引用一个已经被修改过的自定义字体 通过遵循特定的命名标准添加到项目中。第一 将字体文件添加到应用程序项目中的资产文件夹中,然后 设置构建操作:AndroidAsset

在C支持类中设置自定义字体,代码段修改自上述来源:

public class SomeMethod()
{
    new Label
    {
      Text = "Hello, Forms!",
      FontFamily = Device.OnPlatform(null, "Roboto-Light.ttf#Roboto-Light", null)
    }
}
xaml实际上应该是:

<Label Text="Hello Forms with XAML">
    <Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
            <On Platform="Android">Roboto-Light.ttf#Roboto-Light</On>
        </OnPlatform>
    </Label.FontFamily>
</Label>

值得一提的是,如果您的目标API为14或更高版本,roboto主题应该已经是原生android项目的一部分。所以在android上,你实际上可以使用“sans serif light”字体,它实际上就是Roboto。遵循

右键单击,转到属性并检查生成操作。它还应该有一些“资产”在里面。另外,我认为Android不太喜欢连字符“-”符号。尝试删除它。生成操作是AndroidAssetI刚刚执行了该操作,出现了完全相同的错误:Java.Lang.RuntimeException:Font asset not found Roboto-Light.TTFDefinetly handy information。谢谢不过,我不能把它标记为最佳答案,因为它仍然没有解决最初的问题。