简体   繁体   English

WebView显示黑屏

[英]WebView displays black screen

I'm a little embarrassed to post this but I can't seem to figure out where I'm going wrong. 我有点不好意思发布这个,但我似乎无法弄清楚我哪里出错了。 I've looked at every example and every tutorial and everything looks right to me. 我看过每个例子和每个教程,一切看起来都对我不错。 Here's what I'm doing. 这就是我正在做的事情。 I have a listview that when you click on an item it will take you to a WebView that displays some static formatted text associated with that list entry. 我有一个列表视图,当您单击某个项目时,它将转到WebView,该WebView显示与该列表条目关联的一些静态格式化文本。

I had it all working with a TextView but I wanted to be able to use HTML formatting for the text and figured the WebView was the way to go. 我已经使用了TextView,但我希望能够对文本使用HTML格式,并认为WebView是可行的方法。 Right now it is just supposed to display a generic link for testing purposes but when the viewContent intent starts it just goes to a black screen. 现在它只是为了测试目的而显示一个通用链接,但是当viewContent意图启动时它只会进入黑屏。 I can go back and pick another entry and it also just shows the black screen. 我可以回去选择另一个条目,它也只是显示黑屏。

I'm not sure what code you are going to want to see so here's the viewSection class file and the viewsection.xml layout. 我不确定你想要看到什么代码,所以这里是viewSection类文件和viewsection.xml布局。

viewSection.java: viewSection.java:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class viewSection extends Activity {

       /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       WebView wv;

       setContentView(R.layout.viewsection);

       wv = (WebView) findViewById(R.id.wv1);
       wv.loadData("<a href='x'>Hello World! - 1</a>",
                                               "text/html",
                                               "utf-8");
   }
}

viewsection.xml: viewsection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <WebView android:id="@+id/wv1"
               android:layout_height="wrap_content"
               android:layout_width="fill_parent"
       />
</LinearLayout>

You probably want to set android:layout_height on the WebView to fill_parent . 您可能希望将WebView上的android:layout_height设置为fill_parent I'm not sure if WebView supports wrap_content . 我不确定WebView支持wrap_content

EDIT : You'll want to set the LinearLayout width and height to fill_parent as well. 编辑 :你也想将LinearLayout宽度和高度设置为fill_parent

Also, if you're using very light HTML styling, you can still use a TextView; 此外,如果您使用非常轻的HTML样式,您仍然可以使用TextView; there are samples in the API Demos sample app on how to do this (ie StyledText.java and Link.java ). API演示示例应用程序中有关于如何执行此操作的示例(即StyledText.javaLink.java )。

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

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