TextView 无序列表

在Android中实现无序列表并非直接支持ul/li/ol标签,但可以通过使用HTML实体或其他方法来实现。例如,可以使用HTML实体`•`作为列表符号。`Html.fromHtml()`方法可以帮助加载支持的HTML标签。另外,`BulletSpan`类可以提供正确的缩进,允许在同一个TextView中混合其他格式或非格式化的文本。不过,这种方法需要将列表项分别保存为字符串资源,可能不如HTML那样灵活。

How to add bulleted list to android application?  https://stackoverflow.com/questions/4992794/how-to-add-bulleted-list-to-android-application

一、Tough to do as ul/li/ol are not supported. Fortunately you can use this as syntactic sugar:

&#8226; foo<br/>
&#8226; bar<br/>
&#8226; baz<br/>

&#8226; is the html entity for a list bullet more choices are here http://www.elizabethcastro.com/html/extras/entities.html

more about which tags are supported provided by Mark Murphy (@CommonsWare) http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html Load that up with Html.fromHtml

((TextView)findViewById(R.id.my_text_view)).setText(Html.fromHtml(myHtmlString));

二、

  1. browep explained nice the way over HTML. The provided solution with the html entity can be useful. But it includes only the bullet. If your text wraps, the indent will not be correct.

  2. I found other solutions embedding a web view. That maybe is appropriate for some, but i think its kind of overkill... (The same with using a list view.)

  3. I like the creative approach of Nelson :D, but it does not give you the possibility of adding an unordered list to a text view.

  4. My example of an unordered list with bullets using BulletSpan

    CharSequence t1 = getText(R.string.xxx1);
    SpannableString s1 = new SpannableString(t1);
    s1.setSpan(new BulletSpan(15), 0, t1.length(), 0);
    CharSequence t2 = getText(R.string.xxx2);
    SpannableString s2 = new SpannableString(t2);
    s2.setSpan(new BulletSpan(15), 0, t2.length(), 0);
    textView.setText(TextUtils.concat(s1, s2));
    

Positive:

  • Bullets with correct indent after text wraps.
  • You can combine other formatted or not formatted text in one instance of TextView
  • You can define in the BulletSpan constructor how big the indent should be.

Negative:

  • You have to save every item of the list in a separate string resource. So u can not define your list that comfortable how you could in HTML.

 android textview 自动换行 整齐排版 https://blog.csdn.net/guankai1990/article/details/71647324

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值