大佬教程收集整理的这篇文章主要介绍了使用WebView在Android应用程序中对齐文本但是呈现类似TextView的界面?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
WebView view = new WebView(this); view.loadData("my html with text justification","text/html","utf-8");
我创建了一个自定义视图(丑陋,可能很糟糕),它实现了我在TextView中常用的方法,并修改了WebView的内容以反映这些更改.
对于其他人或者我真正不知道的潜在危险它是有用的,对我来说它有效,我已经在几个项目中使用它并且没有遇到任何问题.唯一的小麻烦是我认为它是一个更大的收费记忆,但如果它只是一两个就没有什么可担心的(如果我错了,请纠正我).
结果如下:
以编程方式设置它的@L_489_5@就像这样简单:
JustifiedTextView J = new JustifiedTextView(); J.setText("insert your text here");
当然,保留它是愚蠢的,所以我还添加了改变字体大小和字体颜色的方法,这些方法基本上都是我使用的TextViews.意思是我可以这样做:
JustifiedTextView J = new JustifiedTextView(); J.setText("insert your text here"); J.setTextColor(Color.RED); J.setTextSize(30);
并获得以下结果(图像被裁剪):
但是,这不是向我们展示它的外观,而是分享你是如何做到的!
我知道我知道.
这是完整的@L_489_5@.它还解决了设置透明背景和将uTF-8字符串加载到视图中时的问题.有关详细信息,请参阅reloadData()中的注释.
public class JustifiedTextView extends WebView{ private String core = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 0px 0px 0px 0px;'>%s</body></html>"; private String textColor = "0,255"; private String text = ""; privatE int textSize = 12; privatE int BACkgroundColor=Color.TRANSPARENT; public JustifiedTextView(Context context,AttributeSet attrs) { super(context,attrs); this.setWebChromeClient(new WebChromeClient(){}); } public void setText(String s){ this.text = s; reloadData(); } @SuppressLint("NewApi") private void reloadData(){ // loadData(...) has a bug showing utf-8 correctly. That's why we need to set it first. this.getSetTings().setDefaultTextEncodingName("utf-8"); this.loadData(String.format(core,textColor,textSize,text),"utf-8"); // set WebView's BACkground color *after* data was loaded. super.setBACkgroundColor(BACkgroundColor); // Hardware rendering breaks BACkground color to work as expected. // Need to use software renderer in that case. if(android.os.build.VERSION.SDK_INT >= 11) this.setLayerType(WebView.LAYER_TYPE_SOFTWARE,null); } public void setTextColor(int heX){ String h = Integer.toHexString(heX); int a = Integer.parseInt(h.subString(0,2),16); int r = Integer.parseInt(h.subString(2,4),16); int g = Integer.parseInt(h.subString(4,6),16); int b = Integer.parseInt(h.subString(6,8),16); textColor = String.format("%d,%d,%d",r,g,b,a); reloadData(); } public void setBACkgroundColor(int heX){ BACkgroundColor = hex; reloadData(); } public void setTextSize(int textSizE){ this.textSize = textSize; reloadData(); } }
以上是大佬教程为你收集整理的使用WebView在Android应用程序中对齐文本但是呈现类似TextView的界面?全部内容,希望文章能够帮你解决使用WebView在Android应用程序中对齐文本但是呈现类似TextView的界面?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。