0

한 번에 안드로이드 응용 프로그램의 모든 텍스트보기 글꼴을 변경하는 방법이 있는지 궁금합니다. 모든 텍스트보기에서 프로그래밍 방식으로 또는 동적으로 작성된 텍스트보기와 XML 레이아웃 파일 (드래그 앤 드롭)을 사용하여 별도로 만든 텍스트보기를 의미합니까?내 응용 프로그램의 모든 textviews 글꼴을 변경합니까?

다른 필수 글꼴로 새 테마를 만들고 사용할 수 있다는 것을 알고 있습니다. 하지만 그 테마는 프로그램 내에서 동적으로 생성 된 텍스트보기에 적용되지만 XML 레이아웃에서는 적용되지 않습니다.

해결 방법이 있는지 또는 각 텍스트보기의 글꼴을 수동으로 변경할 수 있는지 여부를 알려주십시오.

public class FontTextView extends TextView { 

private String mTypefacePath; 

public FontTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setAttrs(context, attrs, defStyle); 
    init(context); 
} 

public FontTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setAttrs(context, attrs, 0); 
    init(context); 
} 

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

private void setAttrs(Context context, AttributeSet attrs, int defStyle) { 
    if (isInEditMode()) 
     return; 
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0); 
    mTypefacePath = a.getString(R.styleable.FontTextView_typeface); 
    a.recycle(); 
} 

private void init(Context context) { 
    if (!TextUtils.isEmpty(mTypefacePath)) { 
     try { 
      setTypeface(Typeface.createFromAsset(context.getAssets(), 
        mTypefacePath)); 
     } catch (Exception ex) { 
      // could not create the typeface from path 
     } 
    } 
}} 

당신은 또한 당신의 typeface 속성을 정의해야합니다

+1

당신은 자신 만의 TextView를 만들고 글꼴을 적용해야합니다. 이제이 TextView를 전체 응용 프로그램에서 사용하십시오 ... –

답변

2

가장 쉬운 방법은 텍스트 뷰 위젯을 확장하는 것이다. 유용한 설명을 보려면 this을 살펴보십시오.

관련 문제