的WebView文字大小 [英] WebView text size

查看:86
本文介绍了的WebView文字大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,当我打开我的网页(无静态内容,在飞行构造的)我看到的字体大小太小的 1 。如果我重装我认为正确 2 。我来回走,并正确地看到它。然后......小。没有一个特定的页面,而不是具体的时间。甚至没有具体的版本:我部署在ICS的设备,没有任何问题,然后改变一些东西(如字体大小),并在这里就是问题所在。同样为部署在8和10模拟器。

我的看法是pretty的简单:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_weight =1
    机器人:方向=垂直>
    <的WebView
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / webviewArticle
        机器人:layout_width =match_parent
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1
        机器人:填充=12dp>
    < /的WebView>
< / LinearLayout中>
 

的HTML建设(内容参数格式的HTML)

 字符串toShow =新的StringBuffer()
    .append(< HTML>中)
    .append(< HEAD>中)
    .append(<风格类型='文本/ css的'>身体{字体大小:大;颜色:#FFFFFF;背景颜色:#000000;}< /风格>中)
    .append(< /头>中)
    .append(的String.Format(
        &LT;身体GT;&LT; H1&GT;%S&LT; / H1&GT;&LT; D​​IV&GT;&LT; IMG SRC =%s的ALT =%s的风格=宽度:100 %%;身高:自动/&GT; &LT; / DIV&GT;&LT; D​​IV style='background-color:#000000;'>%s</div><div><h3>%s</h3></div></body>",
        article.getTitle(),
        article.getImageLink()的toString()
        article.getTitle(),
        article.getContent()代替(%,%25),// HTML
        article.getAuthor()))
    .append(&LT; / HTML&gt;中)
    的ToString();

。mBrowser.getSettings()setBuiltInZoomControls(真正的);
mBrowser.setInitialScale(1);
CompatibilityUtils.setLoadWithOverviewMode(mBrowser,真正的); //一套基于Android版本
。mBrowser.getSettings()setUseWideViewPort(真正的);
mBrowser.loadDataWithBaseURL(打假://在/订单/到/刷新,toShow,text / html的,UTF-8,NULL);
 

任何线索?

附加描述25年12月9日:作为图像显示,web视图认为可用空间是半个屏幕,并相应地规定的文本。这是很奇怪。什么是最奇怪的是,它认为这样的文本(图像和下面的DIV上面的头),而不是图像!

解决方案

  WebSettings设置= webView.getSettings();
 

  settings.setTextSize(WebSettings.TextSize.SMALLEST);
 

  settings.setDefaultFontSize(10); //较大的数字意味着更大的字体大小
 

Sometimes, when I load my page (no static content, constructed on the fly) I see the font size too small 1. If I reload I see it properly 2. I go back and forth and see it properly. And then ... small. Not a specific page, not on specific times. Not even specific version: I deploy on ICS device, no problem, then change something (like the font-size) and here is the problem. Same for deploys at 8 and 10 emulators.

My view is pretty straightforward:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webviewArticle"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="12dp" >
    </WebView>
</LinearLayout>

The HTML construction (the content parameter is formatted html)

String toShow = new StringBuffer()
    .append("<html>")
    .append("<head>")
    .append("<style type='text/css'>body{ font-size: large; color: #FFFFFF; background-color: #000000;}</style>")
    .append("</head>")
    .append(String.format(
        "<body><h1>%s</h1><div><img src='%s' alt='%s' style='width:100%%; height:auto' /></div><div style='background-color:#000000;'>%s</div><div><h3>%s</h3></div></body>",
        article.getTitle(),
        article.getImageLink().toString(),
        article.getTitle(),
        article.getContent().replace("%", "%25"), //html
        article.getAuthor()))
    .append("</html>")
    .toString();

mBrowser.getSettings().setBuiltInZoomControls(true);
mBrowser.setInitialScale(1);
CompatibilityUtils.setLoadWithOverviewMode(mBrowser, true); //set based on android version
mBrowser.getSettings().setUseWideViewPort(true);
mBrowser.loadDataWithBaseURL("fake://in/order/to/refresh", toShow, "text/html", "utf-8",null);

Any hint?

Additional description 12/09/25: as the images suggest, the WebView thinks that the available space is half the screen and lays the text accordingly. This is is odd. What is oddest is that it thinks so for the text (the header above the image and the div below) and not the image!!!

解决方案

WebSettings settings= webView.getSettings();

this

settings.setTextSize(WebSettings.TextSize.SMALLEST);

or

settings.setDefaultFontSize(10);//Larger number means larger font size

这篇关于的WebView文字大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆