이 예제는 Kotlin을 사용하여 Android에서 Webview의 글꼴을 변경하는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 만들고 파일로 이동하시겠습니까? 새 프로젝트를 만들고 새 프로젝트를 만드는 데 필요한 모든 세부 정보를 입력합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
예시
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
3단계 − 자산 폴더를 만듭니다. 자산 폴더를 마우스 오른쪽 버튼으로 클릭>> 새 파일(webView.html) 생성 및 다음 코드 -
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>WebView9</title>
<meta forua="true" http-equiv="Cache-Control" content="max-age=0"/>
<style type="text/css">
@font-face {
font-family: 'Font';
src:url("file:///android_asset/Font.otf")
}
body {
font-family: 'Font', serif;
font-size: medium;
text-align: justify;
color:#ffffff
}
</style>
</head>
<body style="background-color:#212121">
All our dreams can come true, if we have the courage to pursue them.” – Walt Disney.
</body>
</html> 4단계 − src/MainActivity.kt에 다음 코드 추가
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val webView: WebView = findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/webView.html");
}
} 5단계 − androidManifest.xml에 다음 코드 추가
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행 아이콘을 클릭하세요.
도구 모음에서. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오.
