问题
I'm trying to re-write a java android app into monodroid, however I have come across an issue with the background transparency of the webview that I use to display the contents of each screen.
This code works correctly on the java version (Black text on a green background), but in the C# version, the webview's background is black (black rectangle on the green background).
Java Code:
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.GREEN);
WebView webView = new WebView(getApplicationContext());
layout.addView(webView);
setContentView(layout);
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadData("<html><body style='background-color: transparent;'>" +
"Some text...</body></html>", "text/html", "UTF-8");
}
C# Code:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var layout = new LinearLayout(ApplicationContext);
layout.SetBackgroundColor(Color.Green);
var webView = new WebView(ApplicationContext);
layout.AddView(webView);
SetContentView(layout);
webView.Settings.JavaScriptEnabled = true;
webView.SetBackgroundColor(Android.Resource.Color.Transparent);
webView.LoadData("<html><body style='background-color: transparent;'>" +
"Some text...</body></html>", "text/html", "UTF-8");
}
I know that there are similar issues that people where having, but they usually were that java version not working. My C# is having the problems though...
I am using the default project template in both cases.
What am I forgetting or not doing?
回答1:
It looks like the value for Android.Resource.Color.Transparent is wrong.
Try:
webView.SetBackgroundColor(0);
or:
webView.SetBackgroundColor(new Color (0, 0, 0, 0));
Update:
Actually, the issue is you are using Android.Resource.Color.Transparent instead of Android.Graphics.Color.Transparent. Resource is a resource id, not a color.
Having said that, Android.Graphics.Color.Transparent doesn't work either. It's encoded as 0xFFFFFF00 which apparently isn't transparent for Android. I've switched it to 0x00000000 for the next release.
The code above will work for now.
来源:https://stackoverflow.com/questions/8646883/monodroid-transparent-webview