透明的WebView不工作在Android 4.0版 [英] Transparent WebView not working on Android v4.0

查看:356
本文介绍了透明的WebView不工作在Android 4.0版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景对这个问题是在这里。它涉及到围绕Android的一个已知的bug,其中的WebView背景需要透明的工作。
<一href=\"http://stackoverflow.com/questions/5003156/android-webview-style-background-colortransparent-ignored-on-android-2-2\">Android的WebView风格背景颜色:透明忽略在Android 2.2

它涉及到的WebView,举办具有透明背景的HTML文档,这样的WebView是透明的,HTML文件可以覆盖到其他意见。

添加下面的方法到的WebView子类,并从构造函数调用它为我的作品在Android V2,V3和V4,除了当web视图的像素高度比像素的屏幕高度较大(如web视图是在滚动型,所以比屏幕更长)。

 保护无效setBackgroundToTransparent(){
    this.setBackgroundColor(Color.TRANSPARENT);
    this.setBackgroundDrawable(NULL);
    如果(Build.VERSION.SDK_INT&GT; = 11)//的Andr​​oid V3.0 +
        尝试{
         方法方法= View.class.getMethod(setLayerType,int.class,Paint.class);
         method.invoke(这一点,1,新的油漆()); // 1 = LAYER_TYPE_SOFTWARE(API11)
        }赶上(例外五){}
}


解决方案

我有一个类似的问题与Android API级别16,这解决了它:

  view.setLayerType(View.LAYER_TYPE_SOFTWARE,NULL); //视图是的WebView

这禁用根据文档视图中的硬件加速:
<一href=\"http://developer.android.com/guide/topics/graphics/hardware-accel.html\">http://developer.android.com/guide/topics/graphics/hardware-accel.html

我知道这是不是要走的路,但由于这是一个已知的问题,他们的util拿出一个妥善的解决办法,这一块做这项工作(至少对我来说)。

Some background to this question is here. It relates to working around a known bug in Android where the WebView background needs to be transparent. Android WebView style background-color:transparent ignored on android 2.2

It involves a WebView, hosting an HTML document with a transparent background, so the WebView is transparent and the HTML document can be overlaid onto other views.

Adding the following method to the WebView subclass and calling it from the constructor works for me on Android v2, v3, and v4, EXCEPT when the pixel height of the WebView is larger than the screen height in pixels (e.g. the WebView is in a ScrollView, so longer than the screen).

protected void setBackgroundToTransparent() {
    this.setBackgroundColor(Color.TRANSPARENT);
    this.setBackgroundDrawable(null);
    if (Build.VERSION.SDK_INT >= 11) // Android v3.0+
        try {
         Method method = View.class.getMethod("setLayerType", int.class,  Paint.class);
         method.invoke(this, 1, new Paint());  // 1 = LAYER_TYPE_SOFTWARE (API11)
        } catch (Exception e) {}
}

解决方案

I had a similar problem with android api level 16 and this solved it:

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // 'view' is a WebView

This disables the hardware acceleration on the view according to the documentation: http://developer.android.com/guide/topics/graphics/hardware-accel.html

I know that it's not the way to go, but since this is an known issue, util they come up with a proper solution, this one does the job (at least for me).

这篇关于透明的WebView不工作在Android 4.0版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆