Is it possible to have multiple styles inside a TextView?

后端 未结 18 2064
醉梦人生
醉梦人生 2020-11-21 17:34

Is it possible to set multiple styles for different pieces of text inside a TextView?

For instance, I am setting the text as follows:

tv.setText(line         


        
18条回答
  •  野的像风
    2020-11-21 18:00

    Using an auxiliary Spannable Class as Android String Resources shares at the bottom of the webpage. You can approach this by creatingCharSquences and giving them a style.

    But in the example they give us, is just for bold, italic, and even colorize text. I needed to wrap several styles in aCharSequence in order to set them in a TextView. So to that Class (I named it CharSequenceStyles) I just added this function.

    public static CharSequence applyGroup(LinkedList content){
        SpannableStringBuilder text = new SpannableStringBuilder();
        for (CharSequence item : content) {
            text.append(item);
        }
        return text;
    }
    

    And in the view I added this.

                message.push(postMessageText);
                message.push(limitDebtAmount);
                message.push(pretMessageText);
                TextView.setText(CharSequenceStyles.applyGroup(message));
    

    I hope this help you!

提交回复
热议问题