2016-10-26 6 views
1

작동하지 않습니다사용자 정의 글꼴이 코드를 작성했습니다

src/main/assets/font/ 
src/main/assets/ 

내가 뭘 잘못하고 있니?

+3

글꼴 패밀리의 안드로이드 지원 .ttf 파일 및 자산 폴더에만 .ttf 파일 넣기 –

+0

Andoid는 TTF (또는) OTF 형식 만 지원합니다. –

+0

@NiteshPareek 완료되었지만 이제 오류가 발생합니다. 가상 메서드 'void를 호출하려고 시도합니다. android.widget.TextView.setTypeface (android.graphics) .Typeface) 'NULL 개체 참조에 – xRobot

답변

0

getAssets()getApplicationContext().

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(), "lato.woff2"); 
0

당신은 assests 경로에서 글꼴 파일을 추가하는 경우 :

Typeface mTypeface == Typeface.createFromAsset(getApplicationContext().getAssets(), "lato.woff2"); 

당신이 assests/글꼴/폴더 경로에 글꼴을 추가하는 경우 : 당신은 할 수 있습니다

Typeface mTypeface == Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/lato.woff2"); 
0

하지 않음 Calligraphy 라이브러리를 사용한다면 바퀴를 재발 명하십시오.

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
          .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf") 
          .setFontAttrId(R.attr.fontPath) 
          .build() 

을 그리고 (앱의 BaseActivity에서 그것을 선언 할 것이다 경우는 전체 앱 작동 상황에 주입 : 당신이해야 할 일은 다음과 같이 응용 프로그램 클래스에서 onCreate() 메소드를 오버라이드 (override)입니다) :

@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
} 

지금이처럼 UI 요소에서 사용자 정의 글꼴을 사용할 수 있습니다 :

<TextView 
    android:text="@string/hello_world" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    fontPath="fonts/Roboto-Bold.ttf"/> 

는 매니페스트에서 응용 프로그램의 클래스를 지정하는 것을 잊지 마십시오. 행운을 빕니다!

0

당신은 또한 생성자 재정 의하여 사용자 정의 텍스트 뷰를 만들 수 있습니다/SRC/메인/자산/글꼴에 TFF 파일이있는 경우 :

import android.content.Context; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 


public class OpenSansTextView extends TextView { 
    public OpenSansTextView(Context context, AttributeSet attributeSet){ 
     super(context, attributeSet); 
     this.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf")); 
    } 
} 

와 다음 XML 레이아웃 사용 :

<YOUR-PACKAGE-NAME.OpenSansTextView 
      android:id="@+id/some_id" 
      android:text="@string/my_string" 
      android:textSize="16sp" /> 
관련 문제