今天下午我一直在用头撞墙,试图让 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),并且只会在模拟器中显示黑屏。最后我想从本地文件中读取。
包含在清单标记下,并且 XML 架构与 Hello Webview 示例相同。
我在这里遗漏了一些明显的东西吗? :(
尝试改变
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)