2016-05-14 54 views
0

我正在嘗試將字體樣式更改爲Cambria,這是我的Android應用程序中活動的標題/動作欄上的瓷磚。對於這個我是這樣的:如何更改活動瓷磚的字體樣式

TextView tv = new TextView(MainActivity.this); 
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Cambria"); 
tv.setText("My App"); 
tv.setTypeface(type); 
actionBar.setCustomView(tv); 

但是這是行不通的。好心幫忙。

+1

您好像缺少文件擴展名。你確定這應該是'字體/ Cambria'而不是'字體/ Cambria.ttf'或類似的東西嗎?另外,您是否購買了此字體的許可證,因爲[Cambria是一種商業字體](https://en.wikipedia.org/wiki/Cambria_%28typeface%29)? – CommonsWare

回答

0

對於您的問題有很多解決方案,但我更喜歡您創建自定義textview並在您的xml和代碼中使用該textview。

public class customText extends TextView { 

public customText (Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 

public customText (Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public customText (Context context) { 
    super(context); 
    init(); 
} 

public void init() { 
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Cambria.ttf"); 
    setTypeface(tf ,1); 

} 
} 
+0

有沒有簡單的方法來改變Android中的字體樣式,就像在Java中一樣? –