Monodroid Transparent WebView

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:21:22

问题


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

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