2013-01-24 3 views
0

내 applcation android에 새 글꼴을 적용 해보고 style.xml에서 호출 할 수 있습니다.응용 프로그램에 새 글꼴 적용

예 : 내 글꼴 thaoma.ttf

style.xml

<style name="text_black_color_bold" > 
     <item name="android:textColor">#3E3E3E</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:typeface"></item> /* call new font here */ 
    </style> 
+0

아니요. 그렇게 할 수 없습니다. 기본 안드로이드 글꼴 만 설정할 수 있습니다. – Ahmad

+0

어떻게 이것이 새로운 글꼴을 적용합니까? thanks –

답변

1

당신은 XML에서 그것을 포함 할 수 없습니다/자산입니다. 당신과 같이 코드를 통해 그것을 할 수 있습니다

final TextView myTextView = getTextView(); 
final AssetManager assets = ctx.getAssets(); 
final Typeface font = Typeface.createFromAsset(assets, "thamoa.ttf"); 
setTypeface(font); 

하나의 멋진 트릭 텍스트 뷰를 확장하고 런타임에 글꼴을 자동으로 적용하는 것입니다.

public class CustomTextView extends TextView { 

    public CustomTextView(Context ctx) { 
     super(ctx); 
     setupText(ctx); 
    } 

    public CustomTextView(Context ctx, AttributeSet attrs) { 
     super(ctx, attrs); 
     setupText(ctx); 
    } 

    public CustomTextView(Context ctx, AttributeSet attrs, int defStyle) { 
     super(ctx, attrs, defStyle); 
     setupText(ctx); 
    } 

    private void setupText(Context ctx) { 
     // check if in edit mode and return. Fonts can't be applied when viewing from editor 
     if(isInEditMode()) { 
      return; 
     } 

     final AssetManager assets = ctx.getAssets(); 
     final TypeFace font = Typeface.createFromAsset(assets, "thamoa.ttf"); 
     setTypeface(font); 
    } 
} 

그런 다음 당신은 같은 방법을 사용하지만, XML에과 같이 참조 할 수 있습니다 :

<package.containing.class.CustomTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    <!-- whatever attributes that would normally apply here --> 
/> 
+1

당신의 서체를 한 번 불러 와서 SoftReferences에 저장하는 유틸리티 클래스를 만든 다음 사용자 정의보기에서 그 방식으로 글꼴에 액세스하도록하는 것이 좋습니다. 다음은이를 수행하는 사람의 예입니다. (필자가 직접 프로젝트를 수정했으나 시작되었습니다.) http://helpmeco.de/2012/2/custom-fonts-in-android-widgets – Karakuri

0

는 전체 응용 프로그램이 사용자 정의 글꼴을 설정하려면, 내가 생각하는 경우는 그건 당신의 이 경우 assets 디렉토리를 만들고 customefont.ttf 파일을 해당 디렉토리에 넣습니다.

는 그 후, 당신은 당신의 등급 파일

complie'me.anwarshahriar:calligrapher:1.0' 

을 그것을 컴파일하고 주요 활동에에서 onCreate 방법에서 사용이 LIB 필요

Calligrapher calligrapher = new Calligrapher(this); 
calligrapher.setFont(this, "yourCustomFontHere.ttf", true); 

이 가장 우아한 슈퍼 빠른 방법입니다 하기 위해서.

관련 문제