如何获得webView for 4.0版本的透明背景

Tap*_*esh 21 android webview

我正在寻找一段能够在WebView版本4.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"        
    />
Run Code Online (Sandbox Code Playgroud)

在活动文件中:

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. */
Run Code Online (Sandbox Code Playgroud)

gun*_*nar 40

试试这个:

webView.setBackgroundColor(0x00000000);
Run Code Online (Sandbox Code Playgroud)

并删除android:hardwareAccelerated ="true"
因为这是偏离主题.


小智 11

如果webview是可以scrolable:

将此添加到Manifest:

android:hardwareAccelerated="false"
Run Code Online (Sandbox Code Playgroud)

要么

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

android:background="@android:color/transparent" android:layerType="software"
Run Code Online (Sandbox Code Playgroud)

将以下内容添加到父卷轴视图:

android:layerType="software"
Run Code Online (Sandbox Code Playgroud)