대기업을 향한 디벨롭 블로그

안드로이드 폰트 변경하기 font Change (일괄변경) 본문

나는안드로이드개발자

안드로이드 폰트 변경하기 font Change (일괄변경)

MHY.PRO 2022. 6. 17. 13:29

안드로이드의 텍스트 폰트를 변경해볼거다. 먼저 폰트를 다운받아보자.

 

구글에 무료폰트를 검색하여 들어가면 폰트를 다운받을 수 있다.

 

나는 https://noonnu.cc/

눈누라는 사이트에서 폰트를 다운받았다. 

 

눈누

상업용 무료한글폰트 사이트

noonnu.cc

 

내눈에 정신차렷체라는 폰트가 눈에 띄어 다운받아보기로 했다. 

 

폰트를 다운하면 zip파일을 주는데 압축을 풀면

 

이렇게 두개의 파일이있다. ttf는 윈도우용이고 맥용은 otf이다.

하지만 여기선 안드로이드에서 적용을 시켜야되서 아무파일이나 상관없음 TTF든 OTF든

 

이파일을 안드로이드 스튜디오에서

res 폴더에 폰트폴더를 하나 만들어 주어야한다. Resource type을 꼭 font로 해주어야됨.

 

만들어주었다면 다운받았던 폰트파일을 폴더안에 넣어주면된다.

 

여기서 문제점

res폴더 안을 구성하는 파일들은 모두 소문자가 되어야한다. 다운받았던 폰트파일의 이름이 대문자라면 소문자로 바꿔주면된다.

 

다음 TextView에 폰트를 한번 멕여보자.

android:fontFamily="@font/ef_wakeup"

텍스트뷰안에 설정해주면된다. fontFamily는 폰트를 설정할 수 있고 font폴더안에 ef_wakeup폰트파일을 적용하겠다. 이 뜻 이다.

 

이런식으로 폰트를 정할 수 있다. 

그런데 여기서 TextView 만들때 마다 하나하나 설정해주어야되는데 앱개발을 하면서 모든 TextView는 폰트를 ef_wakeup파일로 지정하고 싶다하면 일괄설정을 해주어야한다.

 

먼저 res폴더안에 values폴더안에 font_styles라는 value resource file을 만들어주자

<style name="customTextViewFontStyle" parent="@android:style/Widget.DeviceDefault.TextView">
    <item name="android:fontFamily">@font/ef_wakeup</item>
</style>

 

 

먼저 style태그안에 name은 변수이름같은거다. parent로 @android:style/Widget.DeviceDefault.TextView 가 되있는데 이게 바로 TextView이다. 컨트롤 누르고 들어가보면 수많은 View들이 있다.  다음 item태그 안에는 폰트를 설정해주면된다. name에는 폰트를 설정할것 이기때문에 fontFamily가 들어가고 태그사이에 폰트파일을 적어주면된다.

 

 

이제 style을 적용해주면된다.

themes.xml파일로 들어가서

<item name="android:textViewStyle">@style/customTextViewFontStyle</item>

이 한줄 추가해주면된다. 

 

이렇게 fontFamily를 직접 설정하지 않아도 기본으로 폰트가 설정되었다. 추가로

font_style.xml

폰트가 아니라 textColor도 검은색을 기본셋팅해줄 수 있다. 다양하게 가능하다.  그리고 TextView뿐만아니라 다양하게 다 가능하다. 

font_style.xml

//  버튼의 텍스트 폰트
<style name="customButtonFontStyle" parent="@android:style/Widget.DeviceDefault.Button.Borderless">
    <item name="android:fontFamily">@font/ef_wakeup</item>
</style>


//    EditText의 폰트
<style name="customEditTextFontStyle" parent="@android:style/Widget.DeviceDefault.EditText">
    <item name="android:fontFamily">@font/ef_wakeup</item>
</style>

//    라디오버튼 폰트
<style name="customRadioButtonFontStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.RadioButton">
    <item name="android:fontFamily">@font/ef_wakeup</item>
</style>

//    체크박스의 폰트
<style name="customCheckboxFontStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.CheckBox">
    <item name="android:fontFamily">@font/ef_wakeup</item>
</style>

 

themes.xml

<!--        themes.xml안에  버튼 , 에딧텍스트 , 라디오버튼 , 체크박스-->
<item name="android:buttonStyle">@style/customButtonFontStyle</item>
<item name="android:editTextStyle">@style/customEditTextFontStyle</item>
<item name="android:radioButtonStyle">@style/customRadioButtonFontStyle</item>
<item name="android:checkboxStyle">@style/customCheckboxFontStyle</item>

 

 

처음에 xml을 다루는게 어렵고 힘겨웠다.

약간 혼자서 이해하는데 이렇게 이해했다. <style>태그에 name(변수이름을) 짓고 parent에 설정할 요소를 입력한다.  그리고 태그안에 설정할 이름을 넣고 ex)"android:textColor" 안에다가는 값을 넣어주면된다. 이렇게 style을 만들고 

 

style총괄 파일인 themes.xml파일안에서 설정해준다. style태그안에 item을 만들어서 name에는 어떤 요소를 설정할건지 ex)"android: buttonStyle" 넣어주고 그 안에는 내가 만들었던 @style+변수이름이렇게 넣어주었다.