[英]Html.fromHtml in DataBinding - Android
I am using from dataBinding
in my project,when I have bellow xml
it good work :我在我的项目中使用了
dataBinding
,当我有波纹管xml
它很好地工作:
<TextView
android:id="@+id/txtDateCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.format(@string/DateCreate,others.created)}" />
But when I change to bellow get me crash:但是当我改变为波纹管让我崩溃时:
<TextView
android:id="@+id/txtDateCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{Html.fromHtml(String.format(@string/DateCreate,others.created))}" />
Here in my string.xml
:在我的
string.xml
:
<resources>
<string name="DateCreate">open : <![CDATA[<b><font color=#FF0000>%s</b>]]></string>
</resources>
Think you need to import html first in the xml认为你需要先在xml中导入html
<data>
<import type="android.text.Html"/>
</data>
<TextView
android:id="@+id/txtDateCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{Html.fromHtml(String.format(@string/DateCreate,others.created))}" />
I think that the view should not have any logic/transformation to display the data.我认为视图不应该有任何逻辑/转换来显示数据。 What I'd recommend to do is creating a BindingAdapter for this:
我建议做的是为此创建一个 BindingAdapter:
@BindingAdapter({"android:htmlText"})
public static void setHtmlTextValue(TextView textView, String htmlText) {
if (htmlText == null)
return;
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(htmlText);
}
textView.setText(result);
}
Then within the layout call it as usual:然后在布局中像往常一样调用它:
<TextView
android:id="@+id/bid_footer"
style="@style/MyApp.TextView.Footer"
android:htmlText="@{viewModel.bidFooter} />
Where viewModel.bidFooter has the logic to get the String/Spanned/Chars with the text, taking into consideration not having any direct dependence to context, activity, etc考虑到对上下文、活动等没有任何直接依赖性, viewModel.bidFooter具有获取带有文本的字符串/跨度/字符的逻辑
The line below is deprecated in Android N (API level 24).以下行在 Android N(API 级别 24)中已弃用。
Html.fromHtml(content)
Now you're supposed to provide two params (content and flag) like below:现在你应该提供两个参数(内容和标志),如下所示:
Html.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
So I'll suggest you use @BindingAdapter like below:所以我建议你像下面这样使用@BindingAdapter :
object BindingUtils {
@JvmStatic
@BindingAdapter("loadHtml")
fun loadHtml(textView: TextView, content: String?){
if (!content.isNullOrEmpty()) {
textView.text = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Html.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(content)
}
}
}
} }
and in your XML file:并在您的 XML 文件中:
<data>
<import type="com.example.utils.BindingUtils"/>
</data>
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:loadHtml="@{String.format(@string/DateCreate,others.created))}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.