0

我对 webview 的透明度有疑问,我阅读了一堆解决方案,但没有一个对我有用,我可能会错过好的一个。

webView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.getPluginState();
webSettings.setPluginState(PluginState.ON);

String customHtml = "<div style=\"padding:0;margin:0;background:transparent;border:none;position:relative;cursor:pointer;overflow:hidden; height:165px;\">    <object ggnoclick ggswfcid name=\"flashobj\" id=\"flashobj\" width=\"100%\" height=\"165\" data=\"http://c.gumgum.com/ads/com/cbs/55thgrammys/grammys_ca_rihanna.swf?modalURL=GGUID\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" wmode=\"transparent\" background=\"transparent\" style=\"background:transparent;\">        <param name=\"movie\" value=\"http://c.gumgum.com/ads/com/cbs/55thgrammys/grammys_ca_rihanna.swf?modalURL=GGUID\" />        <param name=\"AllowScriptAccess\" value=\"always\" />        <param name=\"wmode\" value=\"transparent\" />    </object></div>";

webView.loadData(customHtml, "text/html", "utf-8");
webSettings.setJavaScriptEnabled(true);
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setBackgroundResource(color.transparent);
//webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

当我取消注释最后一行时,所有 webView 甚至我的内容都是透明的,但是如果我注释最后一行,则背景是白色且不透明。

有人有想法吗?

4

1 回答 1

0

这个对我有用(在 Android 2.1 设备和 Android 4.1 设备上测试):

解决方案来自这个线程

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webview = (WebView) findViewById(R.id.webView1);
        if (webview!=null) {
            String content = "Hello World";
            String html = 
"<html>" +
"<body background=\"#00000000\" link=\"white\">" +
"<font color=\"white\">" + 
content + 
"</font>" +
"</body>" +
"</html>";
            webview.loadDataWithBaseURL(null, 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) {
                    e.printStackTrace();
                }
            }
        }
    }
}
于 2013-01-29T06:24:27.530 回答