英文字体工作,但非英语(印地语)字体不显示在webview中

时间:2017-10-13 17:02:12

标签: webview fonts hindi

英文字体在webview中使用HTML格式,但非英文字体(印地语)未在webview中以HTML格式显示。

这是我的源代码。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="in.wptrafficanalyzer.webviewcustomfont"
          android:versionCode="1"
          android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="in.wptrafficanalyzer.webviewcustomfont.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

demo.html

<html>
    <head>
        <meta http-equiv="content-type" content="text/html;" charset="UTF-8">
        <style>
            /** Specify a font named "MyFont",
                and specify the URL where it can be found: */
            @font-face {
                font-family: "MyFont";
                src: url('file:///android_asset/fonts/BLKCHCRY.TTF');
            }
            h2 { font-family:"MyFont"}
            @font-face {
                font-family: "MyFontSD";
                src: url('file:///android_asset/fonts/SHREE-DEV7-1076H.TTF');
            }
            h1 { font-family:"MyFontSD"}
        </style>
    </head>

    <body>
        <h2>
            Welcome to BLACK CHANCERY FONT
        </h2>
        <h1>
            1234567890
            Am{X
        </h1>
    </body>
</html>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" />
</RelativeLayout>

MainActivity.java

package in.wptrafficanalyzer.webviewcustomfont;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView mWebView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Getting reference to WebView of the activity_main layout
            mWebView = (WebView) findViewById(R.id.webview);

            // Loading an HTML page into webview
            mWebView.loadUrl("file:///android_asset/demo.html");
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }

注意:

  1. HTML文件demo.html被复制到资产文件夹

  2. BLKCHCRY.TTF字体文件复制在字体文件夹中

  3. 将SHREE-DEV7-1076H.TTF字体文件复制到字体文件夹中

  4. 下载字体的链接(SHREE-DEV7-1076H.TTFBLKCHCRY.TTF

0 个答案:

没有答案