2016-09-16 59 views
1

我試圖在我的應用程序中添加自定義字體。創建typeface對象並在其中放置字體的方法起作用。現在我想爲自定義字體制作一個更清晰的代碼。Android應用程序的自定義字體

CustomFont.java

public class CustomFont extends AppCompatActivity { 

    private final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Slabo.ttf"); 

    public CustomFont(TextView textView) { 
     textView.setTypeface(typeface); 
    } 
} 

,現在我想這種字體添加到textview

public class MainActivity extends AppCompatActivity { 

    private Typeface typeface; 
    private CustomFont customFont; 
    private TextView textview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //typeface = Typeface.createFromAsset(getAssets(), "fonts/Slabo.ttf"); // works 
     textview = (TextView) findViewById(R.id.textviewTest); 
     //textview.setTypeface(typeface); // works 

     customFont = new CustomFont(textview); // does not work 

    } 
} 

,但如果我跑這個項目,我得到這個異常:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference 

回答

0

首先CustomFont不應該是activity.It應該是正常的類。

public class CustomFont { 

    private Typeface typeface; 
    public CustomFont(Context context) { 
     typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Slabo.ttf"); 
    } 
    public void setFont(TextView textView) { 
     textView.setTypeface(typeface); 
    } 
} 

從你MainActivity

customFont = new CustomFont(this); 
customFont.setFont(textview); 
+0

謝謝你這個作品! :) 你是最棒的! –

1

試試這個:

TextView text = (TextView) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf"); 
text.setTypeface(font); 
+0

我知道它的工作原理,但我想這在一個單獨的類 –

1

你可以像下面的方法在您的commonUtill類更好的澄清

public static void setTypeface(Context mContext, View view, VIEW_TYPE type, 
            TYPE_FACE face, int bold) { 
     View mView = view; 
     Typeface tface = getTypeface(mContext, face); 
     switch (type) { 
      case TEXTVIEW: 
       ((TextView) mView).setTypeface(tface, bold); 
       break; 
      case BUTTON: 
       ((Button) mView).setTypeface(tface, bold); 
       break; 
      case EDITTEXT: 
       ((EditText) mView).setTypeface(tface, bold); 
       break; 
      case RADIOBUTTON: 
       ((RadioButton) mView).setTypeface(tface, bold); 
       break; 
      case CHECKBOX: 
       ((CheckBox) mView).setTypeface(tface, bold); 
       break; 
      case CHECKEDTEXTVIEW: 
       ((CheckedTextView) mView).setTypeface(tface, bold); 
       break; 
      default: 
       break; 
     } 

    } 

像這樣

public static enum TYPE_FACE { 
     CALIBRI, ICON_FONT, BEBAS, AWESOME, BT, TAGLINE, 
     CALLIBRI, WEBLYSLEEK, WEBLYSLEEK_BOLD, ICON_FONT1 

    } 

和你的觀點方法來創建功能。

public static enum VIEW_TYPE { 
     TEXTVIEW, BUTTON, EDITTEXT, RADIOBUTTON, CHECKBOX, CHECKEDTEXTVIEW 
    } 

通過這種方式,你可以很容易地管理你的代碼。

0

創建Java類這樣的..

public class TextViewKarlaBold extends TextView { 
    public TextViewKarlaBold(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

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

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

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

使用這個類作爲觀XML

<hammerapps.views.TextViewKarlaBold // hammerapps.views is My Package name 
       android:id="@+id/txtVIEW" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="VIEW" 
       android:textColor="@color/black" 
       android:textSize="12dp" /> 
0

使用這個庫,如果你想在一個特定的字體你的整個應用 Check here

0
Use Custom Textview 

public class TextView extends android.widget.TextView { 
Context mContext; 
String str; 
boolean isCorner; 
//fonts 
public static Typeface Font_name; 

public TextView(Context context) { 
    super(context); 
    mContext=context; 
    initialiseFont(null); 
} 





public TextView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    mContext=context; 
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0); 
    try { 
     str = ta.getString(R.styleable.TextView_font_family); 
     isCorner=ta.getBoolean(R.styleable.TextView_isCorner,false); 
    } finally { 
     ta.recycle(); 
    } 
    initialiseFont(str); 
} 


private void initialiseFont(String font) { 

    if(font==null || font.equals("")){ 

    } 
    else { 

      Font_name = Typeface.createFromAsset(mContext.getAssets(), "DroidSansFallbackanmol256.ttf"); 
      setTypeface(Font_name); 

} 

}

使用attrs.xml稱之爲

<declare-styleable name="TextView"> 
    <attr name="font_family" format="string"/> 
    <attr name="isCorner" format="boolean"/> 
</declare-styleable> 
相關問題