WebView和loadData显示Android中的白色屏幕约束布局
问题描述:
我试图使用约束和webView和loadData,我得到的只是一个白色屏幕。当我更改为LinearLayout时,它工作。现在我尝试在Constraint中使用LinearLayout,我得到一个白色屏幕。有没有一个解决方案来获得这项工作与约束? 谢谢;WebView和loadData显示Android中的白色屏幕约束布局
MainActivity
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>
答
您所提供的代码为我工作。不过你可以试试这个:
XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
摇篮:
compile 'com.android.support.constraint:constraint-layout:1.0.2'
+0
谢谢你的工作 –
我执行你所提供的代码示例。对我来说,它展示了一个带有表格的webview。 – CodeCameo