Html.fromHtml 在数据绑定中 - Android
Html.fromHtml in DataBinding - Android
我在我的项目中使用来自 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)}" />
但是当我改为吼叫时我崩溃了:
<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))}" />
在我的 string.xml
中:
<resources>
<string name="DateCreate">open : <![CDATA[<b><font color=#FF0000>%s</b>]]></string>
</resources>
认为您需要先在 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))}" />
我认为视图不应该有任何 logic/transformation 来显示数据。我建议做的是为此创建一个 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);
}
然后在布局中像往常一样调用它:
<TextView
android:id="@+id/bid_footer"
style="@style/MyApp.TextView.Footer"
android:htmlText="@{viewModel.bidFooter} />
其中 viewModel.bidFooter 具有通过文本获取 String/Spanned/Chars 的逻辑,考虑到对上下文没有任何直接依赖性,activity, 等等
以下行在 Android N(API 级别 24)中已弃用。
Html.fromHtml(content)
现在您应该提供两个参数(内容和标志),如下所示:
Html.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
所以我建议您使用 @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)
}
}
}
}
并在您的 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))}"/>
我在我的项目中使用来自 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)}" />
但是当我改为吼叫时我崩溃了:
<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))}" />
在我的 string.xml
中:
<resources>
<string name="DateCreate">open : <![CDATA[<b><font color=#FF0000>%s</b>]]></string>
</resources>
认为您需要先在 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))}" />
我认为视图不应该有任何 logic/transformation 来显示数据。我建议做的是为此创建一个 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);
}
然后在布局中像往常一样调用它:
<TextView
android:id="@+id/bid_footer"
style="@style/MyApp.TextView.Footer"
android:htmlText="@{viewModel.bidFooter} />
其中 viewModel.bidFooter 具有通过文本获取 String/Spanned/Chars 的逻辑,考虑到对上下文没有任何直接依赖性,activity, 等等
以下行在 Android N(API 级别 24)中已弃用。
Html.fromHtml(content)
现在您应该提供两个参数(内容和标志),如下所示:
Html.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
所以我建议您使用 @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)
}
}
}
}
并在您的 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))}"/>