스터디/Android+Java

Andorid XML에서 외부 폰트 적용하기 (Custom TextView XML) (2011.12.17)

Dalmangyi 2017. 10. 7.

안드로이드 환경에서 폰트를 변경할때 소스코드 상에서 변경을 한다거나 기본제공 폰트 4가지 정도를 XML에서 사용하는 강좌 밖에 없어서 작성하게 되었습니다. 소스코드에서는 지저분해져서 사용하기 싫을때 사용하시면 매우 유용합니다 ^^.

 

이 방법을 이용하여도 이클립스에서 xml을 볼때 'Graphical Layout'모드로 보아도 적용이 되어 보이진 않습니다....

혹시라도 Eclipse의 ADT를 튜닝하여서 Graphical Layout에서 폰트까지 적용하는 방법을 아시는 분 있으시면 링크를 답변으로 달아주시면 감사하겠습니다.

이런 정보를 접할때마다 느끼는 것은.. 안드로이드도 아직까지 많은 발전이 필요한 OS로 생각되어집니다.

 

 

외부 폰트를 적용하기 위한 단계

1.TextView 재정의.

2.attrs.xml 작성

3.main.xml 적용

 

1.TextView 재정의.

외부 폰트를 XML에서 사용하려면 기존 TextView를 새롭게 정의할 필요가 있습니다.

 

package com.example;

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

public class TextViewPlus extends TextView {
   
private static final String TAG = "TextView";

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

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

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

   
private void setCustomFont(Context ctx, AttributeSet attrs) {
       
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
       
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont
(ctx, customFont);
        a
.recycle();
   
}

   
public boolean setCustomFont(Context ctx, String asset) {
       
Typeface tf = null;
       
try {
        tf
= Typeface.createFromAsset(ctx.getAssets(), asset);  
       
} catch (Exception e) {
           
Log.e(TAG, "Could not get typeface: "+e.getMessage());
           
return false;
       
}

        setTypeface
(tf);  
       
return true;
   
}

}

여기서 중요한것은 패키지 명과 재정의를 한 Class 네임과 재정의 메소드의 내부입니다.

setCoustomFont내부 소스에서 처음보는 TextViewPlus_customFont는 나중에 attrs.xml 파일의 내용입니다.

Class네임은 당연히 TextViewPlus.java가 되겠죠?

패키지명은 main.xml에서 쓰이게 됩니다.

 

2.attrs.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<declare-styleable name="TextViewPlus">
       
<attr name="customFont" format="string"/>
   
</declare-styleable>
</resources>

TextViewPlus태그의 'customFont'속성 구조를 새로 정의하였습니다.

 

3.main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns
:android="http://schemas.android.com/apk/res/android"
    xmlns
:foo="http://schemas.android.com/apk/res/com.example"
    android
:orientation="vertical" android:layout_width="fill_parent"
    android
:layout_height="fill_parent">

   
<com.example.TextViewPlus
        android
:id="@+id/textViewPlus1"
        android
:layout_height="match_parent"
        android
:layout_width="match_parent"
        android
:text="@string/showingOffTheNewTypeface"
        foo
:customFont="saxmono.ttf">
   
</com.example.TextViewPlus>
</LinearLayout>

xmlns로 패키지 장소를 추가해서 우리가 만든 TextViewPlus의 customFont 속성을 추가할 수 있게 만들어줍니다.

기존 TextView가 아닌 우리가 정의한 TextViewPlus를 사용하기 위해서는 패키지 명까지 같이 적어주셔야 합니다. 'com.example.TextViewPlus'

그리고 attrs.xml에서 정의한 속성 customFont를 xmlns을 이용하여 연결해서 사용합니다.

 

 

 

죄송하지만 이번 가이드에서는 예시 프로젝트 파일이 없습니다.

 

댓글