Bold words in a string of strings.xml in Android

前端 未结 9 2096
甜味超标
甜味超标 2020-12-01 02:44

I have a long text in one of the strings at strings.xml. I want to make bold and change the color of some words in that text.

How can I do it?

9条回答
  •  攒了一身酷
    2020-12-01 02:51

    Use html tag inside string resources :-

    
     Your text ]]> 
    
    

    And get bold text from string resources like :-

    private Spanned getSpannedText(String text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
            } else {
                return Html.fromHtml(text);
            }
        }
    
    
     String s = format(context.getResources().getString(R.string.string_resource_name));
     textView.setText(getSpannedText(s));
    

提交回复
热议问题