在实现新闻详情的时候,安卓也有类似富文本的控件,但是使用webview或者textview等方法,每种方法各有优劣,不是很完美。今天给大家介绍一个安卓控件——HtmlTextView,实现详情页图文混排的显示。
1.在gradle引入控件
dependencies {
compile 'org.sufficientlysecure:html-textview:4.0'
}
2.在布局页面使用上面的控件
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="@+id/html_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="@android:style/TextAppearance.Small" />
3.在activity使用相关方法,此处是其他页面网络获取解析传递过来的html内容,HtmlHttpImageGetter方法不需要手动编写(较textview实现的时候不再需要重写、继承ImageGetter方法,可查看上篇博客)
public class ContentActivity extends BaseActivity {
private String htmlContents = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
HtmlTextView htmlTextView = findViewById(R.id.html_text);
final Intent intent = getIntent();
Bundle bundle = intent.getExtras();
htmlContents = bundle.getCharSequence("contents").toString();
htmlTextView.setHtml(htmlContents, new HtmlHttpImageGetter(htmlTextView));
}
}
4.让我们看一下最终实现的效果
有相关疑问可以留言,感谢点赞转发