在android中显示静态html页面

Piy*_*hra 14 android android-webview

我试图在我的资产文件夹中显示一个html文件,但在Web视图中,我看到的是白色空白页面.我只从stackflow得到了类似的例子.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final String mimeType="text/html";
    final String encoding="UTF-8";
    String htmlString="<html><body>";
    Document doc;
    WebView wv= new WebView(this);
    Elements link = null;


    setContentView(wv);
    try{
        InputStream in=getAssets().open("myweb.html");
        byte[] buffer= new byte[in.available()];
        in.read(buffer);
        in.close();
        wv.loadData(new String(buffer), mimeType, encoding);
    }
    catch(IOException e)
    {
        Log.d("MyWebView", e.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

P.M*_*lch 24

您可以使用加载Web视图的内容

// add a webview with id @+id/the_webwiev to your main.xml layout file
WebView wv = (WebView)findViewById(R.id.the_webview);
wv.loadUrl("file:///android_asset/myweb.html");
Run Code Online (Sandbox Code Playgroud)


Cod*_*key 6

嗯,您是否尝试从官方网页上关注WebView示例?这很简单.

http://developer.android.com/resources/tutorials/views/hello-webview.html

我遵循了这一点,并且没有实现WebView的麻烦.对于非常简单的事情,您的代码看起来过于复杂.

如果您的文件名为pmi_help.html(位于/ assets /文件夹中),则使用以下命令加载:

    mWebView.loadUrl("file:///android_asset/pmi_help.html");
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢你的支持,它的工作正常现在我有另一个任务,如果你可以帮助我,我想在Web视图中将图像设置为背景. (2认同)