xamarin 中的透明 WebView

Transparent WebView in xamarin

我搜索了 Transparent WebView,但找不到我的问题的任何答案。

我有这些:

    string str = "Hello. How are you? I am fine.";
    String s = "<html><head><style type='text/css'>body {"
            + "text-align: justify;}</style></head><body dir = rtl>"
            +str
            + "</body></html>";
    webview.LoadDataWithBaseURL("", s, "text/html", "utf-8", null);





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/webView1"
        android:background="@android:color/transparent" />
</LinearLayout>

谢谢

编辑:

谢谢 SushiHangover 我在你的帮助下编辑了 "s"。现在我没有任何问题。谢谢。

string str = "Hello. How are you? I am fine.";
String s = "<html><head><style type='text/css'>body {background-color:#00000000;"
        + "text-align: justify;}</style></head><body dir = rtl>"
        +"<font style='opacity:0.79'>"
        + "<font color='white'>" + str + "</font>"
        + "</body></html>";

1) 不要 在布局 axml 中设置背景颜色:

<android.webkit.WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView" />

2) 在html中设置背景颜色:

<body style='background-color:#00000000;'>

3) 在运行时设置WebView的背景颜色:

WebView.SetBackgroundColor(Color.Transparent);

4) 这应该适用于大多数 API 级别和 WebView 版本组合,如果您发现组合不起作用,您还需要关闭硬件加速,但通常这是只需要旧 API 版本(即 2.x/3.x...),我通常不建议关闭加速...

WebView.SetLayerType(LayerType.Software, null);

示例:

string htmlContent = "Hello. How are you? I am fine.";
string htmlString = @"
    <html>
        <head>Whosebug</head>
        <body style='background-color:#00000000;'>
            <div>
                " + htmlContent + @"
            </div>
        </body>
    </html>
";
webview.SetBackgroundColor(Color.Transparent);
webview.LoadDataWithBaseURL("", htmlString, "text/html", "UTF-8", null);