今天下午我一直在用头撞墙试图让 WebView 工作。下面是主类中的代码:
public class fkyougoogle extends Activity {
/** Called when the activity is first created. */
WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
// WORKS
//webview.loadUrl("http://www.google.com");
// DOESN'T WORK
//webview.loadUrl("http://www.theregister.co.uk");
//webview.loadData("<html><body>hello</body></html>", "text/html", "utf-8");
//webview.loadDataWithBaseURL("fake://", "<html><body>hello</body></html>", "text/html", "utf-8", "http://www.theregister.co.uk/");
}
}
这是 Google 的“Hello, Webview”示例。如果我使用 WebView 并尝试访问 www.google.com 那么它工作正常。如果我尝试访问任何其他站点,那么它会失败,包括 loadData,它只会在模拟器中显示黑屏。最后我想从本地文件中读取。
包含在 list 标签下,XML 架构与 Hello Webview 示例相同。
我在这里遗漏了什么明显的东西吗? :(
最佳答案
尝试改变
android:layout_width="wrap_content"
android:layout_height="wrap_content"
到
android:layout_width="fill_parent"
android:layout_height="fill_parent"
在你的 main.xml 顶层 LinearLayout
它应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
关于android - 为什么Android WebView显示黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1981507/