android webview setbackground() doesn't work

2019-09-04 07:43发布

问题:

I have a web view on a picture and I want when I click on a button to change the background of the webview to transparent...

To do so I use webView.setBackgroundColor(Color.TRANSPARENT);

When I press the button my webview doesn't change it's color.. I have to write something in it to change it's color after I press the button..

Does somebody knows how to fix this?

Thank you!

回答1:

   WebView.setBackgroundColor(0);
   WebView.setBackgroundResource(Color.TRANSPARENT);

Update:: be sure to have Javascript enabled!



回答2:

I can confirm that the following solution works on every droid I tested (2.1, 2.3, 3.2, 4.0.3, 4.0.4, 4.1.1, 4.1.2):

webview.loadDataWithBaseURL(null, "<html><body background=\"#00000000\" link=\"white\"><font color=\"white\">" + content + "</font></body</html>", "text/html", "UTF-8", null);
webview.setBackgroundColor(0x00000000);
if (Build.VERSION.SDK_INT >= 11) // Android v3.0+
 try {
  Method method = View.class.getMethod("setLayerType", int.class, Paint.class);
  method.invoke(webview, 1, new Paint()); // 1 = LAYER_TYPE_SOFTWARE (API11)
 } catch (Exception e) {
}


回答3:

You can use the WebView.setBackgroundColor(#00000000); It worked for me once. You can take help of this link Click HERE