Android WebView内容显示问题

时间:2015-05-12 13:35:40

标签: android webview

我已将文本加载到WebView

String html = "<p>1</p>\n" +
                "<p>2</p>\n" +
                "<p>3</p>\n" +
                "<p>4</p>\n" +
                "<p>5</p>\n" +
                "<p>6</p>\n" +
                "<p>7</p>\n" +
                "<p>8</p>\n" +
                "<p>9</p>\n" +
                "<p>10</p>\n" +
                "<p>11</p>\n" +
                "<p>12</p>\n" +
                "<p>13</p>\n" +
                "<p>14</p>\n" +
                "<p>15</p>\n" +
                "<p>16</p>\n" +
                "<p>17</p>\n"+
                "<p>18</p>\n";
        questionTextView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

当我有17个段落时,所有内容都显示正常但是当我添加18个以上所有文本时,我只看到滚动线。

这是WebView设置

if (Build.VERSION.SDK_INT >= 11) {
        questionTextView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
  final WebSettings webSettings = questionTextView.getSettings();
        //float fontSize = getResources().getDimension(R.dimen.practice_text_size);
        //webSettings.setDefaultFontSize((int) fontSize);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setBlockNetworkImage(true);
        webSettings.setGeolocationEnabled(false);
        webSettings.setNeedInitialFocus(false);

WebView xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/question_flag"
        android:layout_width="24dip"
        android:layout_height="24dip"
        android:layout_gravity="top|left"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:scaleType="centerInside" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/question_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <de.phase6.vtrainer.custom.FlowLayout
                android:id="@+id/question_images"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:orientation="horizontal"
                android:visibility="gone" />

            <WebView
                android:id="@+id/question_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:paddingRight="8dp" />


            <LinearLayout
                android:id="@+id/question_sounds_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:visibility="gone" />


        </LinearLayout>
    </ScrollView>

    <ImageButton
        android:id="@+id/btn_undo"
        style="@style/BorderlessButton"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_gravity="bottom|start"
        android:longClickable="true"
        android:src="@drawable/back"
        android:visibility="invisible" />

    <ImageButton
        android:id="@+id/btn_send_back"
        style="@style/BorderlessButton"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_gravity="bottom|end"
        android:longClickable="true"
        android:src="@drawable/send_back" />

</FrameLayout>

<View style="@style/DividerHorizontal" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <LinearLayout
        android:id="@+id/input_answer_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/light_grey_ddd"
        android:padding="@dimen/practice_input_padding"
        android:orientation="vertical"
        android:visibility="visible">

        <TextView
            android:id="@+id/user_answer_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/your_answer"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textAllCaps="true"
            android:textColor="@color/grey" />

        <EditText
            android:id="@+id/input_answer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:focusableInTouchMode="true"
            android:focusable="true"
            android:ems="10"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:inputType="textNoSuggestions" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_see_answer"
        style="@style/BorderlessButton"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:textAllCaps="true"
        android:text="@string/show_answer" />

</FrameLayout>

1 个答案:

答案 0 :(得分:1)

经过调查后,我发现当您将WebView放入ScrollView并设置

时,此问题就会重现
if (Build.VERSION.SDK_INT >= 11) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

屏幕上的信息量大约是两个或更多屏幕,然后WebView变为空白。

要解决此问题,请不要设置LayerType或设置webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);