How can I associate css to html string, received from server?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 04:06:11

问题


I'm working on a metro app. Say I receive a string (html) and I load that string in a webview. How can I associate a .css to that html?

Update:

WebViewColumnRight as suggested in an answers is an android method; I'm working on a metro app, c#.


回答1:


As JanivZ said the one way is that your returned html string should contain the reference to the required CSS so that it can be loaded automatically.

or the other option you have is, you can use WebView.loadDataWithBaseURL

htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +    htmlData;
webView.loadDataWithBaseURL(baseUrl, htmlData, "text/html", "UTF-8", null);

After this WebView will be able to find your CSS file from the base URL directory. And if both HTML and CSS are coming from the same base URL then no need to specify a baseURL in webView.loadDataWithBaseURL




回答2:


I would say that the 'returned string' should contain the references necessary to load the css file ( like below for example)

<head>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>

and have the elements in it reference rules defined in that css file. No ?




回答3:


If you are doing a WEBVIEW.NavigateToString(stringHtmlContent) then please inject a

string styleSheetString = "<link rel='stylesheet' type='text/css' href='test.css' />"

strUpdatedHtmlContent = stringHtmlContent.Insert(stringHtmlContent.IndexOf("<head>",styleSheetString);

then do

WEBVIEW.NavigateToString(strUpdatedHtmlContent)

I just hope this helps. O one more thing please take care of the path of the css and all which I just figured from the above discussions...




回答4:


External stylesheet that points to full path where css file can be found.

 <head>
    <link rel="stylesheet" type="text/css" href="full path to css file on server">
    </head>

Internal Style Sheet that comes with your html and requires no dependency on a css file

<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>

Inline Style is a messier way to do what an Internal style sheet does(has no dependency on external css file)

<p style="color:sienna;margin-left:20px">This is a paragraph.</p>


来源:https://stackoverflow.com/questions/14424051/how-can-i-associate-css-to-html-string-received-from-server

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