Android WebView 样式背景颜色:在 android 2.2 上忽略透明
我正在努力创建一个具有透明背景的 WebView。
webView.setBackgroundColor(0x00FFFFFF);
webView.setBackgroundDrawable(myDrawable);
然后我加载一个 html 页面,
<body style="background-color:transparent;" ...
WebView 的背景颜色是透明的,但一旦加载页面,它就会被 html 页面的黑色背景覆盖。这只发生在 android 2.2 上,它适用于 android 2.1。
那么是否需要在 html 页面代码中添加一些内容才能使其真正透明呢?
I'm struggling to create a WebView with transparent background.
webView.setBackgroundColor(0x00FFFFFF);
webView.setBackgroundDrawable(myDrawable);
Then I load a html page with
<body style="background-color:transparent;" ...
The background color of the WebView is transparent but as soon as the page is loaded, it's overwritten by a black background from the html page. This only happens on android 2.2, it works on android 2.1.
So is there something to add in the html page code to make it really transparent ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
这对我有用,
This worked for me,
这个之前提到的问题底部有一个解决方案。
它是 2 个解决方案的组合。
加载 URL 后将此代码添加到 WebViewer 时,它可以工作(API 11+)。
它甚至在硬件加速打开时也能工作
At the bottom of this earlier mentioned issue there is an solution.
It's a combination of 2 solutions.
When adding this code to the WebViewer after loading the url, it works (API 11+).
It even works when hardeware acceleration is ON
我在 2.2 和 2.3 中也遇到了同样的问题。我通过在html中而不是在android中给出alpa值解决了这个问题。我尝试了很多方法,结果发现
setBackgroundColor();
颜色不适用于 alpha 值。webView.setBackgroundColor(Color.argb(128, 0, 0, 0));
将不起作用。所以这是我的解决方案,对我有用。
在 Java 中,
这是下面的输出屏幕截图。
I had the same issue with 2.2 and also in 2.3. I solved the problem by giving the alpa value in html not in android. I tried many things and what I found out is
setBackgroundColor();
color doesnt work with alpha value.webView.setBackgroundColor(Color.argb(128, 0, 0, 0));
will not work.so here is my solution, worked for me.
And in Java,
And here is the Output screenshot below.
实际上这是一个错误,到目前为止还没有人找到解决方法。已创建一个问题。虫子仍然在蜂窝中。
如果您认为重要,请为其加注星标:http://code.google。 com/p/android/issues/detail?id=14749
Actually it's a bug and nobody found a workaround so far. An issue has been created. The bug is still here in honeycomb.
Please star it if you think it's important : http://code.google.com/p/android/issues/detail?id=14749
这是你的做法:
首先让你的项目基于11,但在AndroidManifest中将minSdkVersion设置为8
android:hardwareAccelerated =“false”是不必要的,并且它与8不兼容
为了安全起见,请将此放在你的风格中:
在2.2上为我工作和 4
This is how you do it:
First make your project base on 11, but in AndroidManifest set minSdkVersion to 8
android:hardwareAccelerated="false" is unnecessary, and it's incompatible with 8
For safety put this in your style:
worked for me on 2.2 and 4
最重要的一点没有提到。
html必须具有
body
标记,并将background-color
设置为transparent
。所以完整的解决方案是:
HTML
Activity
The most important thing was not mentioned.
The html must have a
body
tag withbackground-color
set totransparent
.So the full solution would be:
HTML
Activity
下面的代码工作正常Android 3.0+,但是当您在 android 3.0 以下尝试此代码时,您的应用程序将被强制关闭。
您在低于 API 11 上尝试以下代码。
或者
您也可以尝试下面的代码,该代码适用于所有API。
上面的代码对我来说是完整的。
below code works fine Android 3.0+ but when you try this code below android 3.0 then your app forcefully closed.
You try below code on your less then API 11.
Or
you can also try below code which works on all API fine.
above code use full for me.
尝试
webView.setBackgroundColor(0);
Try
webView.setBackgroundColor(0);
以下代码对我有用,尽管我有多个网络视图并且它们之间的滚动有点缓慢。
Following code work for me, though i have multiple webviews and scrolling between them is bit sluggish.
用这个
Use this
webView.setBackgroundColor(Color.TRANSPARENT)
在loadUrl()
/loadData()
之前或之后。android:hardwareAccelerated="false"
。在冰淇淋三明治上进行测试
webView.setBackgroundColor(Color.TRANSPARENT)
before or afterloadUrl()
/loadData()
.android:hardwareAccelerated="false"
in the manifest.Tested on IceCream Sandwich
只需使用这些行......
并记住一点,在 webview 中加载数据后始终设置背景颜色。
Just use these lines .....
And remember a point that Always set background color after loading data in webview.
这肯定会起作用..
使用 Editbackground 在 XML 中设置背景。
现在将显示背景
this will definitely work..
set background in XML with Editbackground.
Now that background will be shown
这不起作用,
将
webview
背景颜色设置为有效此外,我将窗口背景可绘制设置为透明
This didn't work,
Setting the
webview
background color as workedAdditionally, I set window background drawable as transparent
加载 html 后设置 bg(从快速测试来看,加载 html 似乎会重置 bg 颜色..这是针对 2.3 的)。
如果你从已经获得的数据加载html,只需执行一个.postDelayed,其中你只需设置bg(例如透明)就足够了。
set the bg after loading the html(from quick tests it seems loading the html resets the bg color.. this is for 2.3).
if you're loading the html from data you already got, just doing a .postDelayed in which you just set the bg(to for example transparent) is enough..
如果 webview 是可滚动的:
将其添加到清单中:
或
将以下内容添加到布局中的 WebView:
将以下内容添加到父滚动视图:
If webview is scrollable:
Add this to the Manifest:
OR
Add the following to WebView in the layout:
Add the following to the parents scroll view:
尝试
Try
我试图在我的 GL 视图上放置一个透明的 HTML 覆盖层,但它总是闪烁黑色,覆盖了我的 GL 视图。经过几天的尝试摆脱这种闪烁,我发现这个解决方法对我来说是可以接受的(但对 Android 来说是一种耻辱)。
问题是我需要硬件加速来实现漂亮的 CSS 动画,因此
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
不适合我。诀窍是在我的 GL 视图和 HTML 覆盖层之间放置第二个(空)
WebView
。我告诉这个dummyWebView
在 SW 模式下渲染,现在我的 HTML 覆盖在 HW 下渲染平滑,不再有黑色闪烁。我不知道这是否适用于我的宏碁 Iconia A700 以外的其他设备,但我希望我可以帮助别人解决这个问题。
I was trying to put a transparent HTML overlay over my GL view but it has always black flickering which covers my GL view. After several days trying to get rid of this flickering I found this workaround which is acceptable for me (but a shame for android).
The problem is that I need hardware acceleration for my nice CSS animations and so
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
is not an option for me.The trick was to put a second (empty)
WebView
between my GL view and the HTML overlay. ThisdummyWebView
I told to render in SW mode, and now my HTML overlays renders smooth in HW and no more black flickering.I don't know if this works on other devices than My Acer Iconia A700, but I hope I could help someone with this.
这对我有用。尝试在加载数据后设置背景颜色。对于 webview 对象上的 setWebViewClient ,例如:
This worked for me. try setting the background color after the data is loaded. for that setWebViewClient on your webview object like:
试用:
Try out:
如果没有任何帮助,那么很可能您已经固定大小的
webView
,将宽度和高度更改为wrap_content或match_parent
,它应该可以工作。当我尝试加载 Gif 时,这对我有用。If nothing helps, then most probably you have fixed sized
webView
, change the width and height to wrap_content ormatch_parent
, it should work. That worked for me when I tried to load a Gif.您可以像这样使用 BindingAdapter:
Java
XML:
资源
You can user BindingAdapter like this:
Java
XML:
Resources
是最好的答案。有用!
is the best answer. It works!