2016-09-29 58 views
0

我想創建字體類和我使用它在Android Studio中創建的字體類型,使用它

public class Fonts extends Activity{ 


public static Typeface typeface; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/dn.ttf"); 



} 

}

,並用它例如

 Typeface typeface = Fonts.typeface; 

    Button btn_open = (Button)findViewById(R.id.bt_open); 
    btn_open.setTypeface(typeface); 

,但忽略了最低改變!

在manifests.xml Fonts.java是發射活動

我該怎麼辦? 謝謝

+0

https://code.tutsplus.com/tutorials/quick-tip-customize-android-fonts--mobile-1601看看這裏 –

+0

它不是我想創建字體類! – amin

回答

0

這是因爲你隱藏你的靜態變量,在你的onCreate()方法中再次聲明它。 您需要刪除的聲明:

public class Fonts extends Activity { 
    public static Typeface typeface; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Remove the "Typeface" declaration. 
     typeface = Typeface.createFromAsset(getAssets(), "fonts/dn.ttf"); 
    } 
} 

個人建議:避免使用一個靜態變量來訪問您的Typeface。 創建一種方法,例如,您從外部傳遞Context以獲取Typeface或將其初始化爲Application

相關問題