问题
I searched Transparent WebView but I could not find any answer for my problem.
I have these:
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>
With thanks
EDIT:
Thank you SushiHangover I used your help and I edited "s". Now I do not have any problem. Thank you.
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:
1) Do not set a background color in the layout axml:
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
2) Set the background color in html:
<body style='background-color:#00000000;'>
3) Set the background color of the WebView at runtime:
WebView.SetBackgroundColor(Color.Transparent);
4) This should work on most API level and WebView version combinations, if you find a combination that does not work you will also need to turn of hardware acceleration, but typically this is only needed for old API version (i.e. 2.x/3.x...) and I would typically not recommend in turning acceleration off...
WebView.SetLayerType(LayerType.Software, null);
Example:
string htmlContent = "Hello. How are you? I am fine.";
string htmlString = @"
<html>
<head>StackOverFlow</head>
<body style='background-color:#00000000;'>
<div>
" + htmlContent + @"
</div>
</body>
</html>
";
webview.SetBackgroundColor(Color.Transparent);
webview.LoadDataWithBaseURL("", htmlString, "text/html", "UTF-8", null);
来源:https://stackoverflow.com/questions/42328699/transparent-webview-in-xamarin