2014-11-14 110 views
0

我嘗試在textview中使用roboto黑色字體,但tx.setTypeface(tf);正在引發錯誤。它說,「Syntax error on token(s), misplaced construct(s)」,並在(tf)Syntax error on token "tf", VariableDeclaratorId expected after this token」。Android自定義字體加載錯誤

下面是我使用來改變字體的代碼片段:

TextView tx = (TextView) findViewById(R.id.moomoo); 
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Black.ttf"); 
tx.setTypeface(tf); 

據我所知,我只需要改變java文件,同時具有在我的資產文件夾中的TTF字體文件,在名爲「字體」的資產文件夾的子項中。然後我只需要一個對文本視圖的引用,並且可以從java類的構造函數中爲我想要更改的xml文件的字體執行所有操作,對嗎?我的邏輯是否有缺陷,我做錯了什麼? Android是版本19.

+0

是你的TF不爲空? – 2014-11-14 15:27:26

回答

1

變化

Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Black.ttf"); 

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Black.ttf"); 
+0

這對我有很大的幫助,也沒有在我的Android應用程序的構造中,我已經把它放在構造函數的下面,忘記包含它,但移動了支架。再次感謝您的幫助! – Davep 2014-11-14 19:14:12