我在我的布局中添加了一个WebView来显示对齐的文本.我想将WebView的背景设置为透明,使其看起来像textView.这是我做的:
WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(0x00000000);
Run Code Online (Sandbox Code Playgroud)
它适用于模拟器,但是当我在我的设备上运行应用程序时它不起作用:我得到的是白色背景.
String textTitleStyling = "<head><style>* {margin:0;padding:0;font-size:20; text-align:justify; color:#FFFFFF;}</style></head>";
String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis + "</h1></body>";
synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8");
synopsis = (WebView) findViewById(R.id.synopsis);
synopsis.getSettings();
synopsis.setBackgroundColor(0);
Run Code Online (Sandbox Code Playgroud) 我有一个WebView(Android 3.0+)的问题,WebView在显示我的黑色背景("闪烁")之前总是显示白色背景.这是我简单的测试代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setBackgroundColor(Color.BLACK);
setContentView(webView);
loadWebView(webView);
webView.loadDataWithBaseURL("localhost://", "<html><head>" +
"<style>body {background-color: #000}img{max-width:100%}</style></head>" +
"<body>" +
"<img src=\"http://developer.android.com/images/practices/actionbar-phone-splitaction.png\" />" +
"</body></html>",
"text/html", "UTF-8", null);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多解决方案来摆脱这个问题,但并不幸运.
PS:如果关闭硬件加速,则不会出现此问题.有人有同样的问题并解决了吗?
谢谢.
android webview hardware-acceleration android-3.0-honeycomb android-4.0-ice-cream-sandwich
这个问题的一些背景是在这里.它涉及解决Android中已知的错误,其中WebView背景需要透明. Android WebView样式background-color:透明在android 2.2上被忽略
它涉及WebView,托管具有透明背景的HTML文档,因此WebView是透明的,HTML文档可以覆盖到其他视图上.
将以下方法添加到WebView子类并从构造函数中调用它在Android v2,v3和v4上工作,当WebView的像素高度大于屏幕高度(例如WebView在ScrollView中)时除外,比屏幕长.)
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) {}
}
Run Code Online (Sandbox Code Playgroud) 我想在imageView上的静态图像上显示webView.
我像这样为webView设置透明色
webview.setBackground(0);
Run Code Online (Sandbox Code Playgroud)
我将背景颜色设置为透明
<html>
<head>
</head>
<body style="background-color:transparent">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但Webview的背景显示为白色.
我展示了任何问题,并尝试以下页面的解决方案,但没有任何改变.
你有什么想法?请帮我.