IAM使用以下代码将数据设置为WebView:
string htmldata = "<div class=\"daily-activities-content\"> \t <h4> Saint Caesarius of Nazianzen </h4> \r\n<h5>Born: 331 </h5>\r\n<h5>Died: 368 </h5>\r\n\r\n\r\n<p>Caesarius lived in what is today known as Turkey. His father was the bishop of Nazianzen, and his brother is St. Gregory of Nazianzen. </p>\r\n<p>Caesarius and his brother Gregory both received an excellent education. Gregory became a priest, and Caesarius went on to become a doctor. He went to Constantinople to complete his studies. Emperor Constantius wanted Caesarius to be his personal physician. Caesarius politely refused and went back to his home city of Nazianzen. </p>\r\n<p>The next Emperor, Julian the Apostate, asked Caesarius to serve him as well. An apostate is someone who gives up their Christian faith. Caesarius refused Julian's request, too. Julian tried to get Caesarius to give up his faith. He offered Caesarius high positions, bribes, and many other things, but Caesarius did not accept them. </p>\r\n<p>In 368 Caesarius was almost killed in an earthquake. He felt God was calling him to a life of prayer. He gave away everything and led a quiet, prayerful life. One year later Caesarius died, and his brother Gregory preached at his funeral. </p></div>";
string description = htmldata.Replace("'", "'");
var htmlSource = new HtmlWebViewSource();
string cssStyle = @"<style>
@font-face {
font-family: 'Poppins';
src: url('file:///android_asset/Poppins-Regular.ttf');
}
body {
color: #FFFFFF;
font-family: 'Poppins', sans-serif;
font-size: 28px;
}
</style>";
string modifiedHtml = $@"<!DOCTYPE html>
<html>
<head>
{cssStyle}
</head>
<body>
{description.Replace("width=\"640\" height=\"360\"", "width=\"350\" height=\"350\"")}
</body>
</html>";
htmlSource.Html = modifiedHtml;
android_webview.Source = htmlSource;
Screenshot:
我的问题:
在将数据设置为UI之前,我已经为WebView内容添加了自定义字体。但是在UI中,自定义字体看起来很糟糕。 poppins-regular是我的自定义字体。
我创建了一个sample项目
来重新创建此问题。
vertical -stacklayout具有grid.row =“ 0”,但没有提供父级网格
Image IC_DailySaintbanner_xx.png在下载中找不到 the是由滚动浏览的滚动浏览的嵌套 没有文字填充的标签Saintname_label 是外部网络浏览量
html的代码 - 主题是指file :: ///android_asset/poppins-regular.ttf,但poppins-regular.ttf字体未显示在下载的原始文件夹中。为了重置我对问题的理解,我将网络浏览量放在网格中。这将导致WebView从网格中获得其宽度和高度,现在可以看到。如果HTML内容超过给定的高度,WebView会实现其自己的滚动。
<ContentPage
x:Class="CatholicBrain.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Grid>
<WebView x:Name="android_webview" />
</Grid>
</ContentPage>
Poppins-Regular.ttf
url()
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
string htmldata = "<div class=\"daily-activities-content\"> \t <h4> Saint Caesarius of Nazianzen </h4> \r\n<h5>Born: 331 </h5>\r\n<h5>Died: 368 </h5>\r\n\r\n\r\n<p>Caesarius lived in what is today known as Turkey. His father was the bishop of Nazianzen, and his brother is St. Gregory of Nazianzen. </p>\r\n<p>Caesarius and his brother Gregory both received an excellent education. Gregory became a priest, and Caesarius went on to become a doctor. He went to Constantinople to complete his studies. Emperor Constantius wanted Caesarius to be his personal physician. Caesarius politely refused and went back to his home city of Nazianzen. </p>\r\n<p>The next Emperor, Julian the Apostate, asked Caesarius to serve him as well. An apostate is someone who gives up their Christian faith. Caesarius refused Julian's request, too. Julian tried to get Caesarius to give up his faith. He offered Caesarius high positions, bribes, and many other things, but Caesarius did not accept them. </p>\r\n<p>In 368 Caesarius was almost killed in an earthquake. He felt God was calling him to a life of prayer. He gave away everything and led a quiet, prayerful life. One year later Caesarius died, and his brother Gregory preached at his funeral. </p></div>";
string description = htmldata.Replace("'", "'");
var htmlSource = new HtmlWebViewSource();
string cssStyle = @"<style>
@font-face {
font-family: 'Poppins';
src: url('Poppins-Regular.ttf');
}
body {
background: #0091DA;
color: #FFFFFF;
font-family: 'Poppins', sans-serif;
font-size: 28px;
}
</style>";
string modifiedHtml = $@"<!DOCTYPE html>
<html>
<head>
{cssStyle}
</head>
<body>
{description.Replace("width=\"640\" height=\"360\"", "width=\"350\" height=\"350\"")}
</body>
</html>";
htmlSource.Html = modifiedHtml;
android_webview.Source = htmlSource;
}
}
WebView
WebView
string description = Details.description;
// Define custom CSS to set font and text color
string htmlContent = $@"
<html>
<head>
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
<style>
@font-face {{
font-family: 'Poppins';
src: url('file:///android_asset/fonts/Poppins-Regular.ttf'); /* For Android */
}}
body {{
background-color: #0091DA; /* Match WebView background */
color: white; /* Set text color */
font-family: 'Poppins', sans-serif;
padding: 10px;
font-size: 20px; /* Increase font size */
line-height: 1.6; /* Improve readability */
}}
h4, h5, p {{
color: white;
}}
</style>
</head>
<body>
{description}
</body>
</html>";
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = htmlContent;
android_webview.Source = htmlSource;
看起来像WebView在设备/仿真器上看起来不好。我建议您尝试使用“视口”标签来控制视口的大小和形状。
当您设置Android_webview的来源时,请尝试将元标记添加到
modifiedHtml
,例如,string modifiedHtml = $@"<!DOCTYPE html>
<html><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
<head>
{cssStyle}
</head>
<body>
{description.Replace("width=\"640\" height=\"360\"", "width=\"350\" height=\"350\"")}
</body>
</html>";
在我这边为Android Emulator工作。希望它有帮助!