2014-04-20 3 views
1

Android에서 서체를 사용하는 올바른 방법은 무엇입니까? 텍스트보기에 사용자 정의 XML 태그를 사용하는 많은 예제가 있습니다. Java에서 일반 텍스트보기로 설정하려고했는데 제대로 작동하므로 사용자 정의 필드를 사용해야하는 이유는 무엇입니까?Android : 사용자 정의 서체를 사용하는 올바른 방법

답변

1

사용자 정의 서체를 사용하는 경우 프로젝트의/assets 디렉토리에 서체를 추가하는 것이 가장 좋습니다.

TextView customTypefaceTextView = (TextView) findViewById(R.id.customTypefaceTextView); 
Typeface customTypeface = Typeface.createFromAsset(getAssets(), "Custom_Typeface.ttf"); 
customTypefaceTextView.setTypeface(customTypeface); 

그냥 당신의 자산을 발견하는 것은 당신의 활동 N 대 조각 사용자 지정 글꼴을 사용하는 경우 당신은 그냥 대신 getActivity().getAssets()를 호출 할 것입니다 그래서 현재 Context 관련이있을 것이라는 점을 기억

: 다음을 수행하는 매우 가볍고 getAssets(). 또한이 extends TextView 당신이 TextView에 사용할 수있는 사용자 지정 글꼴에 대한 실제적인 구현이 도움을하는 클래스를 생성하는 것이 더 현실적 http://code.tutsplus.com/tutorials/customize-android-fonts--mobile-1601

:

가에서 빠른 팁에 대한 참조입니다 당신이 그렇게 좋아하는 사용자 정의 글꼴을 추가 할 S :

public class CustomTitleTextView extends TextView { 

private Context m_classContext   = null; 
private Typeface m_customTypeFace  = null; 

    // Default Constructor 
public CustomTitleTextView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    this.m_classContext = context; 
    createRobotoTitleTextView(); 
} 

    // Default Constructor 
public CustomTitleTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
    this.m_classContext = context; 
    createRobotoTitleTextView(); 
} 

    // Default Constructor 
public CustomTitleTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    this.m_classContext = context; 
    createRobotoTitleTextView(); 
} 

    // Adds the Typeface to the TextView 
private void createRobotoTitleTextView() 
{ 
    m_customTypeFace = Typeface.createFromAsset(m_classContext.getAssets(), "Roboto-Thin.ttf"); 
    this.setTypeface(m_customTypeFace); 

} 


} 

을 그리고 당신이 어떤 레이아웃 XML이 사용할 수

<packagename.CustomTitleTextView 
    android:id="@+id/customTitleTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

업데이트

다음은 사용자 글꼴을 성공적으로 구현 한 몇 가지 방법입니다. 사용자 정의 TextViewextends TextView을 통해 추가하는 방법을 보여주는 샘플은 XML로 추가하는 것만 큼 반드시 필요한 것은 아니며, Activity 또는 Fragment에서 동적으로 수행하는 대신 재사용 가능한 객체로 TextView을 만드는 방법의 골격을 제공합니다.

행운을 빈다.

+0

일부 안드로이드 레벨에서, Assets에서 생성 된 서체는 캐시되지 않고 올바르게 재사용됩니다. 여러 영역에서 서체를 사용하려는 경우 여기에 게시 된 것과 같이 메모리 누수를 피하기 위해 캐싱 메커니즘을 포함시켜야합니다. http://stackoverflow.com/a/17283657/3194316 – rperryng

+0

잘 읽으려고합니다. 지금은하지만 '없이 ' 그런데 왜 그게 필요합니까? 내가 ' <텍스트 뷰 안드로이드와 같은 일반 텍스트 뷰가 : 텍스트 = "@ 문자열/재생"을 안드로이드 : 문자 유형 = "대담"\t \t 안드로이드 : 텍스트 색상 = "#의 FFF" 안드로이드 : TEXTSIZE = "@ dimen/font2" android : layout_width = "wrap_content" android : layout_height = "wrap_content"/> – user3553099

+0

이것은 코드를 재사용 할 수 있도록하기위한 것입니다. 또한 textview를 매개 변수로 사용하고 texview.setTypeface (customTypeFace)를 호출하는 도우미 메서드를 만들 수도 있습니다. 당신의 글꼴이 표시되지 않습니까? 그렇다면 해당 문제를 해결하는 데 도움을 줄 수 있도록 게시물에 코드를 추가하십시오. 사용자 정의 글꼴을 사용하는 몇 가지 일반적인 방법을 보여 드리고 있습니다. –

관련 문제