Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓可以';不要在webview中更改字体_Android_Android Webview_Android Fonts - Fatal编程技术网

Android 安卓可以';不要在webview中更改字体

Android 安卓可以';不要在webview中更改字体,android,android-webview,android-fonts,Android,Android Webview,Android Fonts,我正在尝试更改Web视图元素的字体。我创建了一个网页,并将.ttf文件放在它旁边。我打开那一页,它看起来很好。现在我想使用webview来显示它。因为我使用的是twitter引导,所以我使用loadUrl方法来加载页面 页面确实加载,但字体错误。在我的css样式文件中,我尝试了以下方法(我的html文件位于“www”目录下的“资产”文件夹中,我也将字体放在该文件夹中): 我还尝试在资产文件夹中创建一个“字体”目录,并将我的字体放在那里,然后尝试以下操作: @font-face { fon

我正在尝试更改Web视图元素的字体。我创建了一个网页,并将.ttf文件放在它旁边。我打开那一页,它看起来很好。现在我想使用webview来显示它。因为我使用的是twitter引导,所以我使用
loadUrl
方法来加载页面

页面确实加载,但字体错误。在我的css样式文件中,我尝试了以下方法(我的html文件位于“www”目录下的“资产”文件夹中,我也将字体放在该文件夹中):

我还尝试在资产文件夹中创建一个“字体”目录,并将我的字体放在那里,然后尝试以下操作:

@font-face {
    font-family: 'Far.TitrDF';
    src: url('file:///android_asset/fonts/Far_TitrDF.ttf');
} 


@font-face {
    font-family: 'Far.TitrDF';
    src: url('../fonts/Far_TitrDF.ttf');
}
但是没有成功。有趣的是,其他一切,比如样式,都显示得很好。你能告诉我我做错了什么吗?

String data=“”+
String data="<html><head><meta      http-equiv=\"content-type\" content=\"text/html;\" charset=\"UTF-8\"><style>"+
        "@font-face {"+
          "font-family: \"MyFont\";"+
          "src: url('file:///android_asset/fonts/normal.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"MyFontBold\";"+
          "src: url('file:///android_asset/fonts/bold.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"MyFontItalic\";"+
          "src: url('file:///android_asset/fonts/Italic.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"BoldItalic\";"+
          "src: url('file:///android_asset/fonts/BoldItalic.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "strong { font-family:\"MyFontBold\"; }"+
        "em {font-family :\"MyFontItalic\";}"+
        "body { font-family:\"MyFont\"; }"+
        "body strong em {font-family:\"BoldItalic\";}"+ 
        "body em  strong{font-family:\"BoldItalic\";}"+
        "</style>"+
        "</head>"+
        "<body>"+your description String+ "</body></html>";


        web.loadDataWithBaseURL("file:///android_asset/", data, "text/html",
                "UTF-8", "null");
“@font-face{”+ “字体系列:\“MyFont\”+ “src:url('file:///android_asset/fonts/normal.otf');"+ “字体大小:粗体!重要;”+ "}"+ “@font-face{”+ “字体系列:\“MyFontBold\”+ “src:url('file:///android_asset/fonts/bold.otf');"+ “字体大小:粗体!重要;”+ "}"+ “@font-face{”+ “字体系列:\“MyFontItalic\”+ “src:url('file:///android_asset/fonts/Italic.otf');"+ “字体大小:粗体!重要;”+ "}"+ “@font-face{”+ “字体系列:\“BoldItalic\”+ “src:url('file:///android_asset/fonts/BoldItalic.otf');"+ “字体大小:粗体!重要;”+ "}"+ “强{字体系列:\'MyFontBold\';}”+ “em{font-family:\'MyFontItalic\';}”+ “正文{font系列:\'MyFont\';}”+ “正文强em{font系列:\'BoldItalic\';}”+ “正文em strong{font系列:\'BoldItalic\';}”+ ""+ ""+ “+您的描述字符串+”; web.loadDataWithBaseURL(“file:///android_asset/,数据,“text/html”, “UTF-8”、“空”);
更改web视图字体是个坏主意,若您的数据只是文本,首选的方法是使用TextView和SetTypeface方法


但您仍然可以通过混合html和css以及css中的font命令为web视图使用自定义字体,但字体必须是web字体而不是任何字体,并且在字体url中没有子文件夹

有时您需要显示uri或html类型的外部信息,因为这是使用web视图所必需的。在我的例子中,我需要显示一个html信息,这有一个默认的样式,但webview无法识别字体。
String data="<html><head><meta      http-equiv=\"content-type\" content=\"text/html;\" charset=\"UTF-8\"><style>"+
        "@font-face {"+
          "font-family: \"MyFont\";"+
          "src: url('file:///android_asset/fonts/normal.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"MyFontBold\";"+
          "src: url('file:///android_asset/fonts/bold.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"MyFontItalic\";"+
          "src: url('file:///android_asset/fonts/Italic.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "@font-face {"+
          "font-family: \"BoldItalic\";"+
          "src: url('file:///android_asset/fonts/BoldItalic.otf');"+
          "font-weight:bold !important;"+
        "}"+
        "strong { font-family:\"MyFontBold\"; }"+
        "em {font-family :\"MyFontItalic\";}"+
        "body { font-family:\"MyFont\"; }"+
        "body strong em {font-family:\"BoldItalic\";}"+ 
        "body em  strong{font-family:\"BoldItalic\";}"+
        "</style>"+
        "</head>"+
        "<body>"+your description String+ "</body></html>";


        web.loadDataWithBaseURL("file:///android_asset/", data, "text/html",
                "UTF-8", "null");