How to add bulleted list to android application?

后端 未结 17 1400
抹茶落季
抹茶落季 2020-11-29 19:14

I have googled my question but there is not working answer provided. How do add a bulleted list to my textview.

17条回答
  •  悲&欢浪女
    2020-11-29 20:07

    Went completely overkill and made a custom text view.

    Use it like this:

    
    

    and the code:

    package com.blundell;
    
    import android.content.Context;
    import android.text.Html;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class BulletTextView extends TextView {
        private static final String SPLITTER_CHAR = "--";
        private static final String NEWLINE_CHAR = "
    "; private static final String HTML_BULLETPOINT = "•"; public BulletTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public BulletTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); checkForBulletPointSplitter(); } private void checkForBulletPointSplitter() { String text = (String) getText(); if (text.contains(SPLITTER_CHAR)) { injectBulletPoints(text); } } private void injectBulletPoints(String text) { String newLinedText = addNewLinesBetweenBullets(text); String htmlBulletText = addBulletPoints(newLinedText); setText(Html.fromHtml(htmlBulletText)); } private String addNewLinesBetweenBullets(String text) { String newLinedText = text.replace(SPLITTER_CHAR, NEWLINE_CHAR + SPLITTER_CHAR); newLinedText = newLinedText.replaceFirst(NEWLINE_CHAR, ""); return newLinedText; } private String addBulletPoints(String newLinedText) { return newLinedText.replace(SPLITTER_CHAR, HTML_BULLETPOINT); } }

提交回复
热议问题