【问题标题】:Wrap content with Header and Footer using LoadUrl in a WebView in Android在 Android 的 WebView 中使用 LoadUrl 用页眉和页脚包装内容
【发布时间】:2015-07-27 10:29:59
【问题描述】:

我的assets/html/ 文件夹中有一个header.html 文件和一个footer.html 文件。

这是我的代码,用于创建和填充我的 WebView 内容:

WebView contentView = (WebView) view.findViewById(R.id.contentView);
String contentText = Function.OBJECT.get("info").toString();

contentView.loadData(contextText, "text/html", null);

如何将contextTextheader.htmlfooter.html 包装起来,并在WebView 中显示输出?

【问题讨论】:

    标签: android html webview android-webview loaddata


    【解决方案1】:

    如果我理解正确,你想解析 header.html 和 footer.html 并在两者之间放置一些内容。

    通过使用 WebView.loaddata(),您是在告诉 WebView 使用字符串作为内容,但这不是您想要的。

    更容易制作 index.html,添加页眉/页脚 div,通过 Javascript/Jquery 将 header.html/footer.html 中的内容绑定/附加到这些 div (jquery append external html file into my page) 并使用 WebView.loadUrl( ) 从资产中加载新的 index.html:

    public class MainActivity extends ActionBarActivity {
    
    private WebView mWebView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
    
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    
        String url  =("file:///android_asset/index.html");
    
        mWebView.loadUrl(url);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2011-11-01
      • 2016-10-08
      相关资源
      最近更新 更多