2013-03-21 66 views
21

我正在寻找一块代码,它能够在版本4.0及以上的WebView中获得透明背景。我的代码在2.3版本上运行正常,但在4.0和4.2版本上运行时,它的白色背景已经变成白色。我正在提供适用于版本2.3的代码,但不在4.0和4.2中。请帮帮我。在此先感谢...如何获得webView的4.0版本的透明背景

在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. */ 
+0

我试过这一次,但最终我对WebView和应用程序使用了相同的背景,所以出现了一个幻觉,它是透明的。 – vorrtex 2013-03-21 15:26:03

回答

39

试试这个:

webView.setBackgroundColor(0x00000000); 

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

+0

谢谢......现在它的工作。但它通常是从xml工作(我认为)。 – Tapesh 2013-05-07 12:03:58

+1

只有这个人工作。 – 2014-04-02 11:48:09

+0

@MehulJoisar:然后upvote它:P – gunar 2014-04-02 12:20:31

1

setBackgroundColor(0x00000000); 会让你的WebView透明

11

如果网页流量是scrolable:

一下添加到清单:

android:hardwareAccelerated="false" 

OR

添加以下的WebView布局:

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

将以下内容添加到父母的滚动条l查看:

android:layerType="software" 
+1

设置android:hardwareAccelerated =“false”这对我来说..在android 16 HCL设备..你可以启用它的应用程序,只是禁用它,你使用的活动有webview .. – 2016-01-12 11:32:55