本文介绍了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
</li><li>item 2
</li></ol>
/>
我明白了:
- 项目 1
- 第 2 项
如何将项目符号更改为数字?
How can I change the bullets to numbers?
谢谢.
推荐答案
我认为您必须在代码中执行此操作.我必须继承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 content = 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(content);
这篇关于Android TextView 中的有序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!