问题描述
我在找一个简单的方法来忘记,我使用了一个的WebView
来在我的的TextView $ C对齐文本$ C>。有人曾经改变这个自定义视图?我很清楚,我可以做这样的事情:
I'm looking for a simple way to forget that I'm using a WebView
to have justified text in my TextView
. Has someone made a custom view for this? I'm well aware that I can do something like this:
WebView view = new WebView(this);
view.loadData("my html with text justification","text/html","utf-8");
但是,当你要设置的大小,颜色或的TextView
其他常见特性它变得丑陋。必须有这样做的更方便的方法。
But it gets ugly when you want to set the size, the color or other common properties of the TextView
. There must be a more convenient way of doing it.
推荐答案
有人让我心烦,我承认这一点。我喜欢 TextViews
看起来像 TextViews
在code,即使我使用的是的WebView
作为实现文本对齐的方式:对齐格式,我不想看它这样
It was getting on my nerves, I admit it. I like the TextViews
to look like TextViews
in the code, and even if I'm using a WebView
as the means of achieving the text-align:justified formatting, I don't want to look at it that way.
我创建了一个自定义视图(丑陋的,可能不好)来实现,我从通常使用的方法的的TextView
并修改的WebView的内容,以反映这些的变化。羯羊它是为别人或潜在的危险我真的不知道,对我来说它的工作原理是有用的,我用它的几个项目,没有遇到任何问题。唯一的小麻烦是,我认为它是一个更大的收费内存明智的,但没什么可担心的,如果它只是一个或两个(纠正我,如果我错了)。
I created a custom view (ugly, probably bad) that implements the methods that I commonly use from the TextView
and modifies the content of the WebView in order to reflect those changes.Wether it's useful for someone else or a potential hazard I really don't know, for me it works, I've used it in several projects and haven't run into any issues. The only minor inconvenience is that I assume it as a bigger toll memory-wise but nothing to worry about if it's just one or two (correct me if I'm wrong).
结果如下:
而$ C $下编程设置它是如此简单:
And the code for setting it programmatically is as simple as this:
JustifiedTextView J = new JustifiedTextView();
J.setText("insert your text here");
当然它会是愚蠢的离开它一样,所以我还添加了方法来改变字体大小和字体,颜色,这基本上都是我用TextViews的。这意味着我可以做这样的事情:
Of course it'd be stupid to leave it like that so I also added the methods for changing the font-size and the font-color which are basically all I use TextViews for. Meaning I can do something like this:
JustifiedTextView J = new JustifiedTextView();
J.setText("insert your text here");
J.setTextColor(Color.RED);
J.setTextSize(30);
和得到以下结果(图像裁剪):
And obtain the following result (images are cropped):
但是,这是不是向我们展示它的外观,它的分享如何你做到了!
我知道,我知道。下面是完整的code。它设置透明背景和加载UTF-8字符串到视图时,也解决了问题。见reloadData()评论的细节。
I know, I know.Here's the full code. It also addresses Problems when setting transparent background and loading UTF-8 strings into the view. See the comments in reloadData() for details.
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,0,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), "text/html","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,%d", r, g, b, a);
reloadData();
}
public void setBackgroundColor(int hex){
backgroundColor = hex;
reloadData();
}
public void setTextSize(int textSize){
this.textSize = textSize;
reloadData();
}
}
这篇关于证明在使用web视图,但presenting一个TextView一样的界面,一个Android应用程序的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!