Ordered lists inside an Android TextView

前端 未结 7 1103
星月不相逢
星月不相逢 2020-12-30 06:47

I want to display an ordered list inside a TextView, for example:
1) item 1
2) item 2

Using the following layout:



        
相关标签:
7条回答
  • 2020-12-30 07:24

    I followed the answer by answer by @jk2K and made modifications to his code. As I need to have indentation for each bullet points,

    String[] textArray = {
        "dfsdljjlfsdsdfjsdjldssdfidfsjdljasdfjfds\n",
        "sdfjdfjlkfdjdfkfjiwejojodljfldsjodsjfsdjdlf\n",
        "djsdfjsdffjdflljfjsadfdjfldfjl"
    };
    
    SpannableStringBuilder content = new SpannableStringBuilder();
    int number = 1;
    for (String t1 : textArray) {
        int contentStart = content.length();
    
        content.append(t1);
    
        int contentEnd = content.length();
        content.setSpan(
                new BulletSpan(10),
                contentStart,
                contentEnd,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        );
    
        number++;
    }
    

    I used the BulletSpan class from android library and replaced new LeadingMarginSpan.Standard(0, 66) with new BulletSpan(10) which creates a BulletSpan with width. As mentioned in the BulletSpan class documentation

    BulletSpan(int gapWidth)
    
    Creates a BulletSpan based on a gap width 
    

    So you won't need anymore to append the bullet which is mentioned in the answer by @jk2K,

     String leadingString = number + ". ";
     content.append(leadingString);
    
    0 讨论(0)
提交回复
热议问题