How to convert part of a string to italics?

后端 未结 3 1927
一个人的身影
一个人的身影 2021-01-22 11:44

I want to make animal.getScientificName as Italics(code below). I\'m using a List View and setting the text in the listview. I wan\'t only part of the Text view to

3条回答
  •  无人及你
    2021-01-22 12:08

    You can accomplish this using spans on your text (to expand here on the correct answer in comment by Georgian Benatos).

    Here is a related question, and the answers show you exactly how to do this:

    • Custom sized italic Font using Spannable

    Specifically some sample code in the answer provided by Raghunandan

    String title="My Custom Text!";  
    TextView tv = (TextView) findViewById(R.id.some_id);
    SpannableString ss1=  new SpannableString(title);
    ss1.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss1.length(), 0);
    ss1.setSpan(new RelativeSizeSpan(2f), 0, ss1.length, 0); 
    tv.setText(ss1);
    

    Spans are a very useful feature - they can be used for various common text styles:

    • italics
    • bold
    • underline
    • bullets

    To have a look at the wide range of spans available, take a look at the Android developers page for the android.text.style package:

    • android.text.style | Android Developers

提交回复
热议问题