在Android中将webview背景图像设置为资源图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1260422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:52:05  来源:igfitidea点击:

Setting webview background image to a resource graphic in Android

androidandroid-webviewandroid-uiandroid-resources

提问by

I'm trying to set the background of a webview to a drawable image resource in Android.

我正在尝试将 webview 的背景设置为 Android 中的可绘制图像资源。

From the sdk it seems something like this would work but it doesn't.

从 sdk 看起来像这样的东西会起作用,但它不会。

WebView web = (WebView) findViewById(R.id.webView);

web.setBackgroundResource(R.drawable.backgroundmain);
web.loadData(profile, "text/html", "UTF-8");

Any idea's?

有任何想法吗?

回答by

As always you tend to figure these things out as soon as you ask the question. For the benefit of others, the layout that I'm using, LinearLayout, can take a background parameter that can be a colour or a resource.

与往常一样,您一提出问题就会倾向于弄清楚这些事情。为了他人的利益,我使用的布局 LinearLayout 可以采用背景参数,该参数可以是颜色或资源。

So in my views .xml file I simply added a

所以在我的视图 .xml 文件中,我只是添加了一个

android:background="@+drawable/backgroundmain"

and use

并使用

web.setBackgroundColor(0);

To make the webview transparent to see the background image behind.

使 webview 透明以查看后面的背景图像。

回答by Jonasm

It will work perfectly, if you do something like this:

如果您执行以下操作,它将完美运行:

webView.setBackgroundColor(Color.TRANSPARENT);
webView.setBackgroundResource(R.drawable.your_image);

回答by embo

If not working on Android 3.1, try fix the AndroidManifest.xml:

如果不适用于 Android 3.1,请尝试修复AndroidManifest.xml

android:hardwareAccelerated="false"