WebView和loadData在Android约束布局中显示白屏

时间:2017-04-06 14:14:43

标签: android android-layout webview

我试图使用Constraints和webView以及loadData,我得到的只是一个白色的屏幕。当我改为LinearLayout时,它起作用了。现在我尝试在Constraint中的LinearLayout,我得到一个白色的屏幕。有没有一个解决方案让这个工作与约束? 感谢;

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>

2 个答案:

答案 0 :(得分:1)

您提供的代码对我有用。但是你可以试试这个:

<强> 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'

enter image description here

答案 1 :(得分:-1)

请删除“约束布局”作为父级,并使用linearLayout。