1. 程式人生 > >WebView 載入本地資源中的字型庫

WebView 載入本地資源中的字型庫

從assets中獲取檔案      file:///android_asset/page1.jpg

從drawable中獲取圖片  file:///android_res/drawable/page1.jpg

從font中獲取字型           file:///android_res/font/font.otf

其他型別舉一反三吧

以下內容沒有驗證:

Using the resource id, the format is:

"android.resource://[package]/[res id]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

 在webview本地模板中新增style:

@font-face {
    font-family: 'ky_custom_font';
    src: url('file:///android_res/font/font_siyuanheiti.otf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

 將相應font字型庫放置在font資原始檔夾中,這樣APP和webview都可以使用同一個字型庫來更改字型了。