android 文本内容对齐

想实现文本内容可以采取自定义TextView实现文字两端对齐,也可以采用webview加载的方式实现。本文说的是采用WebView的方式。

private static final String WEBVIEW_CONTENT = "<html><body style='text-align:justify;text-indent:2em;color:#ffffff;font-size:16px;margin:0;'>%s</body></html>";

private String content="段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1段落1。" ;
private String content2="<div>段落2段落2段落2段落2段落2段落2段落2段落2段落2。</div>";
    
...
webview.setBackgroundColor(Color.parseColor("#000000"));
String format = String.format(WEBVIEW_CONTENT, content+content2);
webview.getSettings().setDefaultTextEncodingName("utf-8");
webview.loadDataWithBaseURL(null, format, "text/html", "utf-8", null);

其中WEBVIEW_CONTENT中 text-indent:2em; 属性是段落1的缩进,不需要的话删除,content2中的<div></div>标签对是段落2的缩进,不需要的同样可以删除。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容