woong's

Android 전체 화면 Font 적용하기 본문

Develop/Android

Android 전체 화면 Font 적용하기

dlsdnd345 2016. 2. 14. 13:03
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android 전체 화면 Font 적용하기 


안드로이드에서는 전체화면의 Font 를 적용하는 것을 지원 하지 않습니다 .

(추측으로는 안드이드에서는 사용자의 Font 제한을 막기 위해서 그런것 같습니다 .)


하지만 간혹 앱을 만들다 보면 전체 화면의 Font 를 적용 할 일이 있습니다 .


전체 화면을 한번에 적용은 힘들지만 , 쉽게 할 수 있도록 구현해 보았습니다 .



주의사항


구현전에 간단히 알고 가야 할 것이 있습니다 .

폰트 적용에는 


1. xml  지연 속도 (0.8초) (단점 일일이 TextView 를 수정 해야 한다 . 코드보다 속도가 느리다고 합니다.)

2. 코드 (거의 없다고 합니다.)



커니 안드로이드 참고


를 통해서 할 수있습니다.



빠르게 폰트 적용 하는 방법


1. 전체를 적용 시킬수 있는 코드를 작성


 -BaseActivty

 -BaseFragmentActivity

 -BaseFragment

 -BaseDialog


4개의 Class를 만들었습니다.


아래 코드는 상속을 뺀 나머지는 동일 합니다 .


코드의 설명은 주석으로 대체 합니다.


 -BaseActivty


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * 
 * @author dlsdnd345
 *
 */
public class BaseActivity extends Activity {
    
    private static Typeface mTypeface;
    private static Typeface mTypefaceBold;
 
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
 
        
        if (BaseActivity.mTypeface == null){
            BaseActivity.mTypeface = Typeface.createFromAsset(getAssets(), Config.FontType.SET_FONT);
            BaseActivity.mTypefaceBold = Typeface.createFromAsset(getAssets(), Config.FontType.SET_FONT_BOLD);
        }
        
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        setGlobalFont(root);
    }
    
    /**
     * 최상위 ViewGroup 을 통해서 TextView 를 검색 하여 변경
     * View 안에 감싸져 있는 TextView 는 재귀 함수를 통해 해결
     * @param root
     */
    void setGlobalFont(ViewGroup root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            View child = root.getChildAt(i);
            
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
    
    
    /**
     * 
     * 위 코드와 동일 시점을 변경 하기 위해서 사용
     * 레이아웃을 구성 하고 폰트를 변경하기 위함.
     */
    void setGlobalFont() {
        
        ViewGroup root1 = (ViewGroup) findViewById(android.R.id.content);
               
        for (int i = 0; i < root1.getChildCount(); i++) {
            
            View child = root1.getChildAt(i);
            
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
}
 


 -BaseFragmentActivity

 

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * @author dlsdnd345
 *
 */
public class BaseFragmentActivity extends FragmentActivity {
    
    private static Typeface mTypeface;
    private static Typeface mTypefaceBold;
 
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
 
        
        if (BaseFragmentActivity.mTypeface == null){
            BaseFragmentActivity.mTypeface = Typeface.createFromAsset(getAssets(), Config.FontType.SET_FONT);
            BaseFragmentActivity.mTypefaceBold = Typeface.createFromAsset(getAssets(), Config.FontType.SET_FONT_BOLD);            
        }
 
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        setGlobalFont(root);
    }
    void setGlobalFont(ViewGroup root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            View child = root.getChildAt(i);
            
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
    
    void setGlobalFont() {
        
        ViewGroup root1 = (ViewGroup) findViewById(android.R.id.content);
               
        for (int i = 0; i < root1.getChildCount(); i++) {
            
            View child = root1.getChildAt(i);
            
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
}
 

 



 -BaseFragment



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * 
 * @author dlsdnd345
 *
 */
public class BaseFragment extends Fragment {
 
 
    private static Typeface mTypeface;
    private static Typeface mTypefaceBold;
 
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
 
        if (BaseFragment.mTypeface == null){
            BaseFragment.mTypeface = Typeface.createFromAsset(getActivity().getAssets(), Config.FontType.SET_FONT);
            BaseFragment.mTypefaceBold = Typeface.createFromAsset(getActivity().getAssets(), Config.FontType.SET_FONT_BOLD);
        }
 
        ViewGroup root = (ViewGroup)getActivity().findViewById(android.R.id.content);
        setGlobalFont(root);
    }
 
    void setGlobalFont(ViewGroup root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            
            View child = root.getChildAt(i);
 
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
            }else if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }else if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }else if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
 
 
 
    public void setGlobalFont() {
 
        ViewGroup root1 = (ViewGroup) getActivity().findViewById(android.R.id.content);
 
        for (int i = 0; i < root1.getChildCount(); i++) {
            View child = root1.getChildAt(i);
 
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
 
 
}



 -BaseDialog

 

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * 
 * @author dlsdnd345
 *
 */
public class BaseDialog extends Dialog {
 
    private static Typeface mTypeface;
    private static Typeface mTypefaceBold;
 
    public BaseDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }
 
    private Context context;
 
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
 
        if (BaseDialog.mTypeface == null){
            BaseDialog.mTypeface = Typeface.createFromAsset(context.getAssets(), Config.FontType.SET_FONT);
            BaseDialog.mTypefaceBold = Typeface.createFromAsset(context.getAssets(), Config.FontType.SET_FONT_BOLD);
        }
 
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        setGlobalFont(root);
    }
    void setGlobalFont(ViewGroup root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            View child = root.getChildAt(i);
 
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
 
    void setGlobalFont() {
 
        ViewGroup root1 = (ViewGroup) findViewById(android.R.id.content);
 
        for (int i = 0; i < root1.getChildCount(); i++) {
            View child = root1.getChildAt(i);
 
            if (child instanceof TextView){
 
                if(((TextView)child).getTypeface() != null){
                    if(((TextView)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((TextView)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((TextView)child).setTypeface(mTypeface);
                }
                
            }
            if (child instanceof EditText){
 
                if(((EditText)child).getTypeface() != null){
                    if(((EditText)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((EditText)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((EditText)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof Button){
 
                if(((Button)child).getTypeface() != null){
                    if(((Button)child).getTypeface().getStyle() == Typeface.BOLD){
                        ((Button)child).setTypeface(mTypefaceBold);
                    }
                }else{
                    ((Button)child).setTypeface(mTypeface);
                }
 
            }
            if (child instanceof ViewGroup){
                setGlobalFont((ViewGroup)child);
            }
        }
    }
}
 



 

2. 적용


위 폰트는 각 필요한 이름을 넣어 주시면 되겠습니다.

위 코드를 패키지 안에 넣고 상속 부분을 바꿔주시면 화면의 TextView 가 폰트 적용 되는 것을 볼 수 있습니다 .


한가지 다른 작업이 필요한 부분이 있습니다 .

리스트뷰는 따로 적용이 안되어서 그부분은 수작업이 필요 할 것 같습니다 .


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
51
52
53
54
55
56
57
58
59
60
61
62
package com.handstudio.android.healthup.fonts;
 
import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
import com.handstudio.android.healthup.common.Config;
 
/**
 * 
 * @author dlsdnd345
 *
 */
public class FontUtil {
 
    
    private static Typeface mTypeface;
    
    public static void setTextFontRecursively ( Context context , ViewGroup parent )
    {
        setTextFontRecursively ( context , parent , Config.FontType.SET_FONT );
    }
    public static void setTextFontRecursively ( Context context , ViewGroup parent , String fontName )
    {
        if(mTypeface == null){
            mTypeface = Typeface.createFromAsset(context.getAssets(), Config.FontType.SET_FONT);
        }
        setTextFontRecursively ( context , parent , mTypeface );
    }    
    public static void setTextFontRecursively ( Context context , ViewGroup parent , Typeface typeface )
    {
        
        for ( int i = 0 ; i < parent.getChildCount() ; i++ ){
            View child = parent.getChildAt (i); 
            
            if (child instanceof TextView)
                ((TextView)child).setTypeface(mTypeface);
            else if (child instanceof EditText)
                ((EditText)child).setTypeface(mTypeface);
            else if (child instanceof Button)
                ((Button)child).setTypeface(mTypeface);
            else if (child instanceof ViewGroup)
                setTextFontRecursively(context , (ViewGroup)child, typeface);    
        }
    }
 
    /**
     * 텍스트뷰 폰트 변경
     * @param ctx
     * @param textView
     */
    public static void setTextFontRecursively(Context ctx,TextView textView) {
        
        textView.setTypeface(Typeface.createFromAsset(ctx.getAssets(), Config.FontType.SET_FONT));
    }
    
}
 



위와 같은 클래스를 만들어서 

리스트뷰 어댑터의 getView 최하단에 


1
FontUtil.setTextFontRecursively(mContext, (ViewGroup) view);


아래 코드를 넣어서 전체 화면 폰트를 적용 했습니다 .


'Develop > Android' 카테고리의 다른 글

Android Mail 보내기  (0) 2016.02.14
Android flag FLAG_ACTIVITY_CLEAR_TASK 사용하기  (0) 2016.02.14
Android Intent 로 객체 전달 하기  (0) 2016.02.14
Android Fragment 통신 하기  (0) 2016.02.14
Android 다이얼 or 전화걸기  (0) 2016.02.14
Comments