Program Tip

Android 애플리케이션에 외부 글꼴을 추가하는 방법

programtip 2020. 10. 28. 20:32
반응형

Android 애플리케이션에 외부 글꼴을 추가하는 방법


내 안드로이드 애플리케이션을위한 멋진 글꼴을 찾고있었습니다. 하지만 문제는 어떻게 외부 글꼴을 지원할 수있는 내 안드로이드 애플리케이션을 만들 수 있는가입니다.

감사합니다.


프로젝트의 assets 폴더 아래에 fonts 폴더를 만들고 TTF를 넣어야합니다. 그런 다음 활동 onCreate ()

TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

모든 TTF가 작동하는 것은 아닙니다. 내가 실험하는 동안, 그것은 단지 하위 집합 (Windows에서는 이름이 작은 대문자로 쓰여진 것들)에서만 작동했습니다.


사용자 정의 글꼴로 전체 앱에 대해 사용자 정의 TextView를 사용할 수 있습니다.

public class MyTextView extends TextView {

   Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
   Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);

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

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

   public MyTextView(Context context) {
       super(context);
   }

   public void setTypeface(Typeface tf, int style) {
       if (style == Typeface.BOLD) {
           super.setTypeface(boldTypeface/*, -1*/);
       } else {
           super.setTypeface(normalTypeface/*, -1*/);
       }
   }
}

asset 폴더에 fonts라는 폴더를 만들고 아래 링크에서 스 니펫을 추가하십시오.

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
textview.setTypeface(tf);

구현하려면 아래 샘플을 통해 Typeface를 사용해야합니다.

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
   if (view instanceof TextView) 
   {
      TextView textView = (TextView) view;
      textView.setTypeface(typeface);
      }
   }
}

이를 수행하는 가장 쉬운 방법은 원하는 글꼴을 응용 프로그램과 함께 패키지화하는 것입니다. 이렇게하려면 프로젝트 루트에 assets / 폴더를 만들고 글꼴 (TrueType 또는 TTF 형식)을 자산에 넣으면됩니다. 예를 들어 자산 / 글꼴 /을 만들고 거기에 TTF 파일을 넣을 수 있습니다.

그런 다음 위젯에 해당 글꼴을 사용하도록 지시해야합니다. 안타깝게도 더 이상 레이아웃 XML을 사용할 수 없습니다. XML은 응용 프로그램 자산으로 숨겨둔 글꼴에 대해 알지 못하기 때문입니다. 대신 Typeface.createFromAsset (getAssets (), "fonts / HandmadeTypewriter.ttf")를 호출 한 다음 생성 된 Typeface 객체를 가져와 setTypeface ()를 통해 TextView에 전달하여 Java 코드를 변경해야합니다.

더 많은 참조를 위해 여기에 내가 얻은 튜토리얼이 있습니다.

http://www.androidguys.com/2008/08/18/fun-with-fonts/


I recommend this approach it very nice with adding name of custom font in typeface to styles.xml and putting your set of fonts into assets folder.


One more point in addition to the above answers. When using a font inside a fragment, the typeface instantiation should be done in the onAttach method ( override ) as given below:

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
}

Reason:
There is a short span of time before a fragment is attached to an activity. If CreateFromAsset method is called before attaching fragment to an activity an error occurs.

참고URL : https://stackoverflow.com/questions/5634245/how-to-add-external-fonts-to-android-application

반응형