简体   繁体   中英

Is it possible to inject css into a TextView that is using Html.FromHtml?

I have a TextView that is getting it's text from Html.FromHtml.

public override void OnViewCreated(View view, Bundle savedInstanceState)
{
      ISpanned marketingText = Html.FromHtml(ViewModel.Html);
      var text = view.FindViewById<TextView>(Resource.Id.marketingText);
      text.TextFormatted = marketingText;
}

Is it possible to inject a CSS file into this? Or should I be using a WebView?

I am using xamarin, c#, but any Java responses are also welcome as it can be translated.

TextViews can only contain a small subset of HTML tags not CSS. These tags include <strong> , <underline >, <emphasis >, <p> , <small> , <medium> etc. There are a few others as well.

You can change the font and font colour by doing something like

<p font-color="blue">This is my blue text</p>

but you can't add CSS. If you need to do formatting in this way that would require CSS then a webview would be better.

While you can't inject CSS into the TextView's HTML, you can however apply certain attributes to the TextView in the XML Editor. For example: android:textColorLink="#ff0000" will set all <a href HTML tags in the TextView to be red.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM