WebView无法像浏览器一样呈现CSS吗?

7
据我所知,WebView与内置浏览器是相同的东西,对吗?
我遇到了一个问题,一个包含一些绝对定位的div元素的页面,所有元素都会堆叠在一起,而不是找到其正确位置,并且background-image完全被忽略了。
话虽如此,在我手机上的浏览器(HTC Incredible S-2.3.3,库存浏览器)上正确呈现它,而且使用嵌入式Webview的应用程序也能够指向该页面并正确呈现。这导致我相信我在应用程序中拥有的webview出了问题。
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.*;

public class ShowWebView extends Activity {

    public WebView web;
    public String baseURL = "http://test.dev/";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webpage);

        web = (WebView) findViewById(R.id.webpage);
        home = (Button) findViewById(R.id.tool_Home);
        back = (ImageButton) findViewById(R.id.tool_Back);
        forward = (ImageButton) findViewById(R.id.tool_Forward);
        refresh = (ImageButton) findViewById(R.id.tool_Refresh);

        ajax = (ImageView) findViewById(R.id.test_anim);
        ajax.setBackgroundResource(R.drawable.ajax_animation);


        // Settings
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        web.setWebViewClient(new WebViewClient());

        Bundle extras = getIntent().getExtras();
        if(extras != null) {

            String url = baseURL+extras.getString("page")+"?androidApplication"; // The Basics, page gets added to baseURL
            String id = "";
            String push = "false"; // false by default

            // If an ID exists, lets get it
            if(extras.getString("id") != null) {
                id = extras.getString("id");
            }

            if(extras.getString("push") != null) {
                push = extras.getString("push");
            }

            // Build the URL
            if(id != "") url = url + "&id="+id;
            if(push != "false")     url = url + "&pushVersion";

            web.loadUrl(url);
        } else {
            web.loadUrl("http://www.bing.com");
        }
}

以下是我的WebView XML:

<?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"
    style="@style/MainPage"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/Header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="match_parent"
            android:layout_height="42sp"
            android:scaleType="fitXY"
            android:src="@drawable/top_header" />

    </LinearLayout>
    <LinearLayout
        android:id="@+id/SubHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="28sp"
            android:scaleType="fitXY"
            android:src="@drawable/top_sub_header" />

    </LinearLayout>

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








        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:orientation="vertical" >

            <WebView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/webpage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 />

        </LinearLayout>




        <LinearLayout
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="40sp"
            android:layout_marginTop="2.5sp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/tool_Home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Home" />


            <ImageButton
                android:id="@+id/tool_Back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/backward" />


            <ImageButton
                android:id="@+id/tool_Forward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/forward" />


            <ImageButton
                android:id="@+id/tool_Refresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/refresh" />


            <ImageView
                android:id="@+id/test_anim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerHorizontal="true" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

任何帮助解决这个问题都将是真正了不起的!

1
我可以提供一些故障排除的建议,以帮助获取更多信息。1)当您说它在手机浏览器上正确呈现时,只是为了确保,这个手机是否与您运行应用程序的地方相同?不同版本的Android中的CSS支持不同。2)如果您删除所有视图,并且仅在布局中有一个WebView,是否会改变任何内容?3)如果您将其他具有背景位置和绝对位置(可能是具有HTML / CSS示例的网站)的网站打开到您的WebView中,是否存在相同的问题,还是只有您的HTML / CSS? - cottonBallPaws
谢谢您的回复,它让我找到了正确的解决方法!我已经找到了问题所在,请查看我上面的问题(在最顶部)获取答案 :) - RedactedProfile
谢谢。帮了我很多。setLayoutAlgorithm(LayoutAlgorithm.NORMAL) 对我很有用。 - jannej
5
您应该将您发现的答案作为下面的回答插入并接受它,而不是将它作为问题的编辑。 - XML
1个回答

5

显然:

web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

这行代码导致webview崩溃,因为它重新安排html渲染器将所有内容放在单列中(或者试图这样做)。禁用此行或使用NORMAL可以使渲染正常。

我仅使用此行代码来禁用水平滚动。所以现在我仍然需要解决那个问题。


我尝试了很多方法,最后这个方法终于奏效了。如果你遇到了CSS问题,请试试这个方法。谢谢! - Jason
是的,这行代码解决了CSS渲染问题:web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);,对这个答案点赞。 - blueware
WebSettings.LayoutAlgorithm.NARROW_COLUMNS is actually deprecated, use WebSettings.LayoutAlgorithm.NORMAL - Lorenzo Vincenzi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接