티스토리 뷰
반응형
    
    
    
  Android 8.0 (API 26)부터 폰트를 XML로 정의하여 사용할 수 있습니다. 글꼴 파일을 res/font/폴더에 넣고 글꼴을 정의하는 XML 파일을 추가한 후 @font/myfont 혹은 R.font/myfont 형태로 사용할 수 있습니다. Support Library 26을 사용하면 Android 4.1 (API 16) 이상의 기기에서도 사용할 수 있습니다.
폰트를 XML로 정의하기
- res/font 폴더에 폰트를 추가합니다. 
폰트파일은 기본적으로 TTF 폰트를 지원하며, OTF는 Android 1.6 부터 지원합니다. - res/font 폴더에 XML 파일을 추가합니다. 파일명이 폰트의 리소스명이 됩니다.
 - XML파일을 열고 폰트를 정의하는 내용을 작성합니다.
<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:font="@font/arita4b" /> <font android:font="@font/arita4l" /> <font android:font="@font/arita4m" /> <font android:font="@font/arita4sb" /> </font-family> - 정의된 폰트를 사용하는 방법은 다른 리소스 파일을 사용하는 것과 동일합니다.
XML 레이아웃 파일에서 사용
코틀린 코드에서 사용<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/arita4"/>val typeface = resources.getFont(R.font.arita4) textView.typeface = typeface 
Android 4.1 (API 16) 지원하기
Android 4.1 (API 16) 이상의 장치에서 지원하려면 Support Library를 사용해야합니다. 추가적으로 다음 사항을 유의해주세요.
- 폰트를 정의하는 XML파일에서는 app 네임스페이스를 사용해주세요.
<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:app="http://schemas.android.com/apk/res-auto"> <font app:fontStyle="normal" app:fontWeight="200" app:font="@font/arita4l" /> <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/arita4m" /> <font app:fontStyle="normal" app:fontWeight="600" app:font="@font/arita4b"/> <font app:fontStyle="normal" app:fontWeight="800" app:font="@font/arita4sb" /> </font-family> - 코틀린파일에서 로드할 때는 ResourcesCompat을 사용해주세요.
val typeface = ResourcesCompat.getFont(context, R.font.arita4) 
References
- Android XML Font : "https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml0
 
'Mobile > Android' 카테고리의 다른 글
| [Android] Style 정의하기 (0) | 2022.07.02 | 
|---|---|
| [Android] drawable과 drawable-v21 폴더만 생성되는 이유 (0) | 2021.04.12 | 
| [Android] drawable과 mipmap (1) | 2018.05.03 | 
| [Xamarin.Android] Push Notification 구현 (0) | 2018.04.23 | 
| [Android] 반투명 상태바 사용 (0) | 2017.11.17 | 
| [Android] View와 Layout (0) | 2017.11.14 | 
| [Android] Lollipop 5.0 (API 21)에서의 문제 (0) | 2017.11.14 | 
					댓글
						
					
					
					
				
			
									최근에 올라온 글
									
							
								
								
									최근에 달린 댓글
									
							
								
								
									TAG
									
							
								
								- ASP.NET Core
 - Android
 - Xamarin.Forms 요약
 - Xamarin
 - windows
 - material-ui
 - React
 - Vue
 - linux
 - ios
 - .NET Standard
 - Xamarin.iOS
 - Xamarin.Forms
 - npm
 - VisualStudio
 - flutter
 - Xamarin.Forms eBook
 - TypeScript
 - MS SQL
 - WPF
 
- Total
 
- Today
 
- Yesterday