本文介绍了里面一个Android的TextView有序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示里面一个TextView的有序列表,例如:
1)第1项
2)第2项
I want to display an ordered list inside a TextView, for example:
1) item 1
2) item 2
使用以下布局:
<TextView
android:text="<ol><li>item 1\n</li><li>item 2\n</li></ol>
/>
我得到:
- 第1项
- 第二项
我如何可以改变子弹的数字?
How can I change the bullets to numbers?
感谢。
推荐答案
我认为你必须为此在code。我不得不继承LeadingMarginSpan得到这个工作。这里是我做到了。
I think you have to do this in code. I had to subclass LeadingMarginSpan to get this to work. Here is how I did it.
private class NumberIndentSpan implements LeadingMarginSpan {
private final int gapWidth;
private final int leadWidth;
private final int index;
public NumberIndentSpan(int leadGap, int gapWidth, int index) {
this.leadWidth = leadGap;
this.gapWidth = gapWidth;
this.index = index;
}
public int getLeadingMargin(boolean first) {
return leadWidth + gapWidth;
}
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
if (first) {
Paint.Style orgStyle = p.getStyle();
p.setStyle(Paint.Style.FILL);
float width = p.measureText("4.");
c.drawText(index + ".", (leadWidth + x - width / 2) * dir, bottom - p.descent(), p);
p.setStyle(orgStyle);
}
}
}
弄个你的看法,并用它这样的:
Get hold of your view, and use it like this:
SpannableStringBuilder ssb = new SpannableStringBuilder();
for(String text : list) {
int contentStart = content.length();
content.append(text);
content.setSpan(new NumberIndentSpan(15, 15, number), contentStart, content.length(), 0);
}
TextView view = findViewById(R.id.....);
view.setText(ssb);
希望这有助于其他人在寻找这样的: - )
Hope this helps others looking for this :-)
这篇关于里面一个Android的TextView有序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!