我使用html . fromhtml在TextView中查看html。
Spanned result = Html.fromHtml(mNews.getTitle());
...
...
mNewsTitle.setText(result);
但是Html.fromHtml现在在Android N+中已弃用
我怎样才能找到做这件事的新方法?
我使用html . fromhtml在TextView中查看html。
Spanned result = Html.fromHtml(mNews.getTitle());
...
...
mNewsTitle.setText(result);
但是Html.fromHtml现在在Android N+中已弃用
我怎样才能找到做这件事的新方法?
当前回答
如果您有幸在Kotlin上开发, 只需创建一个扩展函数:
fun String.toSpanned(): Spanned {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
@Suppress("DEPRECATION")
return Html.fromHtml(this)
}
}
而且用在任何地方都很贴心:
yourTextView.text = anyString.toSpanned()
其他回答
为了扩展@Rockney和@k2col的答案,改进后的代码可以如下所示:
@NonNull
public static Spanned fromHtml(@NonNull String html) {
if (CompatUtils.isApiNonLowerThan(VERSION_CODES.N)) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
return Html.fromHtml(html);
}
}
其中CompatUtils.isApiNonLowerThan:
public static boolean isApiNonLowerThan(int versionCode) {
return Build.VERSION.SDK_INT >= versionCode;
}
不同之处在于没有额外的局部变量,并且只在else分支中弃用。所以这不会抑制所有的方法,而是单个分支。
它可以帮助谷歌将决定在一些未来版本的Android弃用甚至fromHtml(字符串源,int标志)方法。
我有很多这些警告,我总是使用FROM_HTML_MODE_LEGACY,所以我做了一个名为HtmlCompat的助手类,包含以下内容:
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String source) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(source);
}
}
尝试以下支持基本html标签,包括ul ol li标签。 创建一个标签处理程序,如下所示
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Html.TagHandler;
import android.util.Log;
public class MyTagHandler implements TagHandler {
boolean first= true;
String parent=null;
int index=1;
@Override
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equals("ul")) parent="ul";
else if(tag.equals("ol")) parent="ol";
if(tag.equals("li")){
if(parent.equals("ul")){
if(first){
output.append("\n\t•");
first= false;
}else{
first = true;
}
}
else{
if(first){
output.append("\n\t"+index+". ");
first= false;
index++;
}else{
first = true;
}
}
}
}
}
将文本设置为Activity,如下所示
@SuppressWarnings("deprecation")
public void init(){
try {
TextView help = (TextView) findViewById(R.id.help);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
help.setText(Html.fromHtml(getString(R.string.help_html),Html.FROM_HTML_MODE_LEGACY, null, new MyTagHandler()));
} else {
help.setText(Html.fromHtml(getString(R.string.help_html), null, new MyTagHandler()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
和html文本资源字符串文件作为
<![CDATA[…原始HTML数据…]] >
这是我的解决方案。
if (Build.VERSION.SDK_INT >= 24) {
holder.notificationTitle.setText(Html.fromHtml(notificationSucces.getMessage(), Html.FROM_HTML_MODE_LEGACY));
} else {
holder.notificationTitle.setText(Html.fromHtml(notificationSucces.getMessage()));
}
你可以使用
//noinspection deprecation
return Html.fromHtml(source);
仅对单个语句而不是整个方法抑制检查。