繁体   English   中英

高度为100%的Android Webview和背景图片

[英]Android webview with height:100% and a background image

我有一个应用程序,它从数据库中获取数据,然后将其显示为文章列表。 数据被获取并放入带有Web视图的recyclerView中。

一篇特定的文章包含以下html:

<div style="position: absolute; height: 100%; background: url('https://www.softray.it/wp-content/uploads/2015/10/home-test-6.2.png') no-repeat;">
    <div style="height: 100%; width: 50%; margin-left: auto; margin-right: auto;display: table-cell; vertical-align: middle; text-align: center;">
        <h1 style="padding: 10px; text-align: center; font-family: 'Open Sans', sans-serif; color: #ffffff;">SOFTRAY TECHNOLOGY</h1>
        <p style="padding-left: 5px; text-align: center; color: #ffffff; font-family: 'Open Sans', sans-serif;">La potenza del mobile re-inventa le applicazioni business e un nuovo stile di IT</p>
    </div>
</div>

*很抱歉,凌乱的html我只是在数据库上没有像样的编辑工具。

这里的问题是我的背景图像没有显示,甚至第二个div也没有以应有的方式垂直居中。

我尝试在显示数据之前将htmlbody标签附加到我的数据上,但仍然没有背景图片加载。

holder.webView.loadData("<html style='height:100%;'><body style='height:100%;margin:0;padding:0;'>" + s + "</body></html>", "text/html", "utf-8");

我知道我可能必须给元素一个固定的高度,但是我想适应任何屏幕。 有小费吗?

编辑:我只需要获取Webview内容的高度(以像素为单位),就可以在html标记中进行设置。 问题在于,在加载数据之前,我需要了解此维度。

尝试将HTML代码作为example.html文件保存在资产文件夹中,然后使用加载Web视图:

webView.loadUrl("file:///android_asset/example.html");

网页视图应使用match_parent作为宽度和高度,以覆盖其父布局的100%。

编辑:

从服务器获取数据时,可以实现此方法:

private void saveHtmlFile(String htmlStr) {

    String path = "/data/data/" + getApplicationContext().getPackageName() + "/files/";
    String fileName = "example";
    fileName = fileName + ".html";
    File file = new File(path, fileName);

    try {
        FileOutputStream out = new FileOutputStream(file);
        byte[] data = htmlStr.getBytes();
        out.write(data);
        out.close();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///data/data/" + getApplicationContext().getPackageName() + "/files/example.html");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

请一次检查此链接:

https://stackoverflow.com/a/15617341/6388699

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM