Android使用HtmlTextView控件实现新闻详情图文混排

在实现新闻详情的时候,安卓也有类似富文本的控件,但是使用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.让我们看一下最终实现的效果

 

 

有相关疑问可以留言,感谢点赞转发

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值