【发布时间】:2017-09-01 15:18:01
【问题描述】:
我试图使用约束、webView 和 loadData,但我得到的只是一个白屏。当我更改为 LinearLayout 时,它起作用了。现在我尝试在约束内使用 LinearLayout,我得到一个白屏。是否有解决方案可以使用约束来完成这项工作? 谢谢;
主活动
package com.example.grady.webviewdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.myWebView);
String myHtmlString = "<html> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>" +
"<h2>This is a HTML Heading in Android WebView.</h2>\n" +
"<p>This is a HTML paragraph in android WebView.</p>\n" +
"\n" +
"<h4>Following is HTML Table in WebView</h4>\n" +
"<table border=\"1\" width=\"100%\">\n" +
" <tr>\n" +
" <td>Android</td>\n" +
" <td>WebView</td>\t\t\n" +
" <td>100</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Android</td>\n" +
" <td>WebView</td>\t\t\n" +
" <td>200</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Android</td>\n" +
" <td>WebView</td>\t\t\n" +
" <td>300</td>\n" +
" </tr>\n" +
"</table>" +
"</body></html>";
mWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.grady.webviewdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/myWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
我已经执行了您提供的代码示例。对我来说,它显示的是带有表格的 webview。
标签: android android-layout webview