android 폰트 일괄 적용


BaseActivity에서 폰트일괄적용을 하고, 다른 액티비티가 BaseActivity를 상속받아 사용하면 안드로이드 폰트가 일괄 적용 된다.


[[ XML Layout 사용시 ]]


<< BaseActivity.java >>


public class BaseActivity extends Activity {
	private static Typeface mTypeface = null;

	@Override
	public void setContentView(int layoutResID) {
	    super.setContentView(layoutResID);
		if (mTypeface == null) {
			//mTypeface = Typeface.createFromAsset(this.getAssets(), "fonts/NanumGothic.ttf"); // 외부폰트 사용
			mTypeface = Typeface.MONOSPACE;	// 내장 폰트 사용
		}
		setGlobalFont(getWindow().getDecorView());
		// 또는
		// View view = findViewById(android.R.id.content);
		// setGlobalFont(view);
	}

	private void setGlobalFont(View view) {
		if (view != null) {
			if(view instanceof ViewGroup){
				ViewGroup vg = (ViewGroup)view;
				int vgCnt = vg.getChildCount();
				for(int i=0; i < vgCnt; i++){
					View v = vg.getChildAt(i);
					if(v instanceof TextView){
						((TextView) v).setTypeface(mTypeface);
					}
					setGlobalFont(v);
				}
			}
		}
	}
}


<< MainActivity.java >>


public class MainActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}


[[ inflate 사용시 ]]

getView, getChildView 같은 inflate 하는데에서...

<< Utilities.java >>

public class Utilities {

	public static void setGlobalFont(View view) {
		if (view != null) {
			if (view instanceof ViewGroup) {
				ViewGroup vg = (ViewGroup) view;
				int len = vg.getChildCount();
				for (int i = 0; i < len; i++) {
					View v = vg.getChildAt(i);
					if (v instanceof TextView) {
						((TextView) v).setTypeface(Typeface.MONOSPACE);
					}
					setGlobalFont(v);
				}
			}
		} else {
			Log.e("setGlobalFont", "This is null  ");
		}

	}

}

<< MyActivity.java >>

@Override
public View getChildView(int gpos, int cpos, boolean isLastChild, View cv, ViewGroup pv) {
LinearLayout subLayout = (LinearLayout) cv;
	if (subLayout == null) {
		subLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.child_item, null);
		Utilities.setGlobalFont(subLayout);	// 폰트 적용
		//...
	}
//...
}

참고 사이트 :

http://networkedblogs.com/HoWou

http://androidhuman.tistory.com/497


+ Recent posts