2017-07-14 36 views
0

我有以下XAML代碼:了java.lang.RuntimeException:字體資產未發現的Roboto光強(初級)

<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:找不到字體資產的Roboto - 輕」即使字體被放置在Assets文件夾:

Solution Explorer

我能做些什麼來解決這個問題?

+0

右鍵單擊,轉到屬性並檢查生成操作。它也應該有一些「資產」的東西。另外,我認爲Android不太喜歡連字符(' - '符號)。嘗試刪除它。 –

+0

構建操作是AndroidAsset – Radu

回答

0

在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上這個名字並檢查其全名屬性。

enter image description here

+0

我剛剛這樣做,並出現完全相同的錯誤:Java.Lang.RuntimeException:字體資產未找到Roboto-Light.ttf – Radu

0

你可以找到一個全面的指南,在這個Xamarin Developers Guide Link.

正如史蒂芬強調使用自定義的字體Xamarin.Forms,它確保您的TTF字體文件是在正確的位置是很重要的,在這個實例你看起來是正確的,android項目的位置確實是'資產'文件夾。

Xamarin.Forms爲Android可以引用已經 添加到項目中按照特定的命名標準自定義字體。第一個 將字體文件添加到應用程序項目的Assets文件夾中,並將 設置爲Build Action: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> 

值得一提的是,的Roboto主題應該是原生Android項目的一部分,如果您的目標API爲14或更高。所以在android上,你實際上可以使用'sans-serif-light'字體,它實際上是Roboto。按照this link to see an excellent answer on an android specific thread about the fonts they now include in API 14+

+0

Definetely方便的信息。謝謝!儘管如此,我不能將其標記爲最佳答案,因爲它仍未解決最初的問題。 – Radu