21

我正在寻找一段能够在WebView4.0 及更高版本中获得透明背景的代码。我的代码在 2.3 版上运行良好,但是当我在 4.0 和 4.2 版上运行它时,它会变成白色背景。我提供的代码适用于 2.3 版,但不适用于 4.0 和 4.2。请帮我。提前致谢...

在 XML 中:

<WebView
    android:id="@+id/webView"
    android:layout_marginTop="6dp"
    android:layout_below="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:hardwareAccelerated ="true"        
    />

在活动文件中:

webView = (WebView) findViewById(R.id.webView);
backButton = (ImageView) findViewById(R.id.backButton);
webView.setBackgroundColor(0);                      
webView.loadUrl("file:///android_asset/Info.html");
webView.setBackgroundColor(Color.TRANSPARENT);  
/* this is used to make the background white after loading the file on screen. */
4

3 回答 3

41

试试这个:

webView.setBackgroundColor(0x00000000);

并删除android:hardwareAccelerated ="true"
,因为那是题外话。

于 2013-03-25T14:11:28.453 回答
11

如果 webview 是可滚动的:

将此添加到清单中:

android:hardwareAccelerated="false"

或者

将以下内容添加到布局中的 WebView:

android:background="@android:color/transparent" android:layerType="software"

将以下内容添加到父滚动视图:

android:layerType="software"
于 2013-10-28T19:15:26.727 回答
1

设置背景颜色(0x00000000);将使您的 webview 透明

于 2013-09-23T13:10:13.977 回答