我怎样才能绑定在一个Android应用程序的HTML字符串到web视图? [英] How can I bind an HTML string to a webview in an Android app?

查看:228
本文介绍了我怎样才能绑定在一个Android应用程序的HTML字符串到web视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个安卓的TextView这势必可能会或可能不会包含HTML字符串

 <的TextView
        机器人:文字=中文字
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:自动链接=所有
        机器人:填充=10dp
        风格=@风格/ ListItemText
        本地:MvxBind =TEXT答案/>

不知怎的,我需要能够显示该文本与呈现的HTML。所以我想我会TextView的切换到的WebView并绑定相同的字符串到web视图。我是相当新的Andr​​oid开发人员,所以我不知道这甚至可以做或有另一种方式我应该接近这一点。


解决方案

对于少量的HTML文本,你可以使用一个TextView的TextFormatted财产 - 通常是用ValueConverter将执行HTML解析(使用 Html.FromHtml(输入)

 公共类FromHtmlValueConverter:MvxValueConverter<串GT;
{
   保护覆盖对象转换(字符串值,类型TARGETTYPE,对象参数,CultureInfo的文化)
   {
       返回Html.FromHtml(值);
   }
}<的TextView
    机器人:文字=中文字
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:自动链接=所有
    机器人:填充=10dp
    风格=@风格/ ListItemText
    本地:MvxBind =TextFormatted FromHtml(答案)/>


有关呈现一个完整的WebView,那么你需要添加自定义绑定 - 或继承,并提供一个自定义属性 - 为了调用LoadData虚拟机发生变化时。这类似于在MVVMCross 动态使用的UIWebView继承技术p>

I currently have a android:TextView which is bound to a string which may or may not contain HTML.

<TextView
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:padding="10dp"
        style="@style/ListItemText"
        local:MvxBind="Text Answer" />

Somehow I need to be able to display this text with the html rendered. So I thought I would switch the TextView to a WebView and bind the same string to the WebView. I'm fairly new to Android dev, so I'm not sure if this can even be done or if there is another way I should be approaching this.

解决方案

For small amounts of HTML text, you can use the TextFormatted property of a TextView - usually with a ValueConverter which will perform the HTML parsing (using Html.FromHtml(input))

public class FromHtmlValueConverter : MvxValueConverter<string>
{
   protected override object Convert(string value, Type targetType, object parameter, CultureInfo culture)
   {
       return Html.FromHtml(value);
   }
}

<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all"
    android:padding="10dp"
    style="@style/ListItemText"
    local:MvxBind="TextFormatted FromHtml(Answer)" />


For rendering a full WebView, then you'll need to add a custom binding - or inherit and provide a custom property - in order to call LoadData when the VM changes. This is similar to the inheritance technique used for UIWebView in Dynamic Binding UIWebView in MVVMCross

这篇关于我怎样才能绑定在一个Android应用程序的HTML字符串到web视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆