问题
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