Setting webview background image to a resource graphic in Android

江枫思渺然 提交于 2019-11-28 18:49:20

问题


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

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

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

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

Any idea's?


回答1:


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.

So in my views .xml file I simply added a

android:background="@+drawable/backgroundmain"

and use

web.setBackgroundColor(0);

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




回答2:


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

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



回答3:


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

android:hardwareAccelerated="false"


来源:https://stackoverflow.com/questions/1260422/setting-webview-background-image-to-a-resource-graphic-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!