본문 바로가기

Android

[Android]커스텀 폰트 일괄 적용법

반응형


이전에 올렸던 커스텀 폰트 간단 적용법에서는 각각의 TextView나 EditText에 일일이


fontFamily를 적용시켜야만 했습니다.


한 화면에서 다양한 font들을 적용시켜야하는 경우라면 어쩔 수 없겠지만


앱의 전체적인 폰트를 통일시켜서 사용하는 경우에는 매번 fontFamily를 적용시켜주는게 귀찮을 수 있습니다.


이번 포스팅에서는 style.xml을 이용하여 폰트를 일괄 적용하는 방법에 대해 알아보겠습니다.


우선 아래처럼 style.xml에 폰트를 적용시킬 위젯들의 default 테마를 상속받아서 fontFamily를 적용시켜줍니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    <!-- Custom font style 일괄적용 -->
    <style name="TextViewStyle" parent="@android:style/Widget.DeviceDefault.TextView">
        <item name="android:fontFamily">@font/my_font_family</item>
    </style>
 
    <style name="ButtonStyle" parent="@android:style/Widget.DeviceDefault.Button.Borderless">
        <item name="android:fontFamily">@font/my_font_family</item>
    </style>
 
    <style name="EditTextStyle" parent="@android:style/Widget.DeviceDefault.EditText">
        <item name="android:fontFamily">@font/my_font_family</item>
    </style>
 
    <style name="RadioButtonStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.RadioButton">
        <item name="android:fontFamily">@font/my_font_family</item>
    </style>
 
    <style name="CheckboxStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.CheckBox">
        <item name="android:fontFamily">@font/my_font_family</item>
    </style>
cs




그리고 본인이 사용하고자 하는 테마안의 buttonStyle, editTextStyle, radioButtonStyle, checkboxStyle, textviewStyle들을

위에서 작성한 스타일을 적용시켜줍니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
 
        <item name="android:textViewStyle">@style/TextViewStyle</item>
        <item name="android:buttonStyle">@style/ButtonStyle</item>
        <item name="android:editTextStyle">@style/EditTextStyle</item>
        <item name="android:radioButtonStyle">@style/RadioButtonStyle</item>
        <item name="android:checkboxStyle">@style/CheckboxStyle</item>
    </style>
cs




그러면 레이아웃에서 각각의 TextView에서 textStyle만 바꿔주면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"
        
        android:textStyle="normal"

        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/textView2" />
 
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"
        
        android:textStyle="italic"

        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="@+id/textView3" />
 
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"
        
        android:textStyle="bold"

        app:layout_constraintBottom_toBottomOf="@+id/textView7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toTopOf="@+id/textView7" />
cs




이처럼 style.xml의 테마를 이용하여 커스텀 폰트를 일괄적용 시킬 수 있습니다.


저같은 경우에는 회사에서 같은 normal testStyle이라도 다른 폰트들을 적용시키는 경우가 많아서


각각 다른 fontFamily들을 적용시켰지만


하나의 fontFamily만 사용하시는 분들이라면 이번 방법을 사용하시는게 훨씬 편하실 겁니다.





반응형