android - 如何在 android 中通过 javascript 更改 WebView 内容字体系列?

标签 android html css fonts

我的 android 应用程序有一个设置屏幕,用户可以在其中选择自定义字体并将其应用到任何地方,所有字体都保存在 res/font 目录中......我的问题是一旦选择了一种字体,它就会应用到整个元素,除了 html 文件,因为它们由 css 文件设置样式。

用户使用此屏幕选择字体,更改立即生效,如下所示: enter image description here

我想将用户的字体应用到我的 html 文件中……这就是它们现在的样子 :( enter image description here

我认为这可以通过更改 css 文件中的字体系列来实现。 这是我的 CSS

@media only screen and (max-width: 600px) {
h2 {
  color: blue;
  font-size:20px;
  text-align: justify;
   margin: 10px;
} 
h3 {
  color:  blue;
  font-size:18px;
  text-align: justify;
   margin: 10px;
} 
h4 {
  color:  blue;
  font-size:16px;
  text-align: justify;
   margin: 10px;
} 
p {
    font-size:16px;
    color: black;
    text-align: justify;
     margin: 10px;
}
ul {
    font-size:16px;
    color: black;
    text-align: justify;
     margin: 10px;
}
ol {
    font-size:16px;
    color: black;
    text-align: justify;
     margin: 10px;
}
a:link {
     color:  blue;
     font-size:16px;
     text-decoration: underline;
     text-align: justify;
     margin: 10px;
 }
 a:visited {
     color:  blue;
     font-size:16px;
     text-decoration: underline;
     text-align: justify;
     margin: 10px;
 }
  a:active {
      color:  blue;
      font-size:16px;
      text-decoration: underline;
      text-align: justify;
      margin: 10px;
  }
img {
    display: block;
    margin-left: auto;
    margin-right: 0;
    width: 70%;
}
}
@media only screen and (min-width: 600px) {
h2 {
  color: blue;
  font-size:30px;
  text-align: justify;
   margin: 10px;
}
h3 {
  color:  blue;
  font-size:25px;
  text-align: justify;
   margin: 10px;
}
h4 {
  color:  blue;
  font-size:23px;
  text-align: justify;
   margin: 10px;
}
p {
    font-size:23px;
    color: black;
    text-align: justify;
     margin: 10px;
}
ul {
    font-size:23px;
    color: black;
    text-align: justify;
     margin: 10px;
}
ol {
    font-size:23px;
    color: black;
    text-align: justify;
     margin: 10px;
}
a:link {
     color:  blue;
     font-size:23px;
     text-decoration: underline;
     text-align: justify;
      margin: 10px;
 }
 a:visited {
     color:  blue;
     font-size:23px;
     text-decoration: underline;
     text-align: justify;
      margin: 10px;
 }
  a:active {
      color:  blue;
      font-size:23px;
      text-decoration: underline;
      text-align: justify;
       margin: 10px;
  }
img {
    display: block;
    margin-left: auto;
    margin-right: 0;
    width: 70%;
}
}

这是怎么做到的??

最佳答案

您不能直接通过 android 更改您的 HTML 文件字体系列。 您的问题将由 JavaScript 解决。 遵循我的方法:

首先 :) 使用@font-face 在您的 CSS 文件中将您的自定义字体定义为 bleow

@font-face {
    font-family: 'yourfontName1';
    src: url('android.resource://yourAppPackageName/font/yourfontName1.fontFromat');
    /* font format can be ttf , otf and ...*/
}
@font-face {
    font-family: 'yourfontName2';
    src: url('android.resource://yourAppPackageName/font/yourfontName2.fontFormat');
        /* font format can be ttf , otf and ...*/

}
/* other font definition */

第二个 :)domStorageEnabled 从 false(默认值)更改为 true ,为您的 WebView 启用 JavaScript禁用缓存

mWebview.getSetting().setDomStorageEnabled(true);
mWebview.getSettings().setJavaScriptEnabled (true);
mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

第三个:)

通过您的 WebView 在您的 onItemSelectedListener 或您想要的任何东西中运行一个 JavaScript 函数:

String script= "javascript:(function(){  " +
                        "var cols = document.getElementsByTagName('*');" +
                        "for(i = 0; i < cols.length; i++) {" +
                        "cols[i].style[\"font-family\"]=\""+yourSelectedFontName+"\";" +
                        "}})();";

mWebview.evaluateJavascript(script,null);

这对我有用,我希望对你也有用:)

关于android - 如何在 android 中通过 javascript 更改 WebView 内容字体系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58792383/

相关文章:

android - 获取 Activity 经理的状态

Android - 在不损失质量的情况下缩放、旋转位图并将其保存到 SD 卡

html - :after tag not working properly with <li>

html - 应用于 asp 的 CSS :Label under placeholder1 only working in design view?

html - 无法让背景图片出现在 div 中

java - 获取并显示 Android 上已安装程序的列表

android - ListView 单个项目样式

javascript - 如何在 JavaScript 中更改一个字母的颜色?

html - css 定位,图像被剪切

html - HTML5 中的样式自定义元素