2

有时,当我加载我的页面(没有静态内容,动态构建)时,我看到字体太小1。如果我重新加载,我会正确看到2。我来回走动,正确地看到它。然后……小。不是特定页面,不是特定时间。甚至没有特定版本:我在 ICS 设备上部署,没问题,然后改变一些东西(比如字体大小),这就是问题所在。在 8 和 10 模拟器上部署相同。

我的观点很简单:

<?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>

HTML构造(内容参数为格式化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);

有什么提示吗?

附加说明 25 年 9 月 12 日:如图所示,WebView 认为可用空间是屏幕的一半,并相应地放置文本。这很奇怪。最奇怪的是,它对文本(图像上方的标题和下方的 div)而不是图像这么认为!!!

4

1 回答 1

8
WebSettings settings= webView.getSettings();

settings.setTextSize(WebSettings.TextSize.SMALLEST);

或者

settings.setDefaultFontSize(10);//Larger number means larger font size
于 2012-09-25T00:01:09.910 回答