Is it possible to have multiple styles inside a TextView?

后端 未结 18 1845
醉梦人生
醉梦人生 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:12

    In case, anyone is wondering how to do this, here's one way: (Thanks to Mark again!)

    mBox = new TextView(context);
    mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" + 
                "<small>" + description + "</small>" + "<br />" + 
                "<small>" + DateAdded + "</small>"));
    

    For an unofficial list of tags supported by this method, refer to this link or this question: Which HTML tags are supported by Android TextView?

    0 讨论(0)
  • 2020-11-21 18:13

    Try Html.fromHtml(), and mark up your text with bold and italic HTML tags e.g:

    Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");
    textView.setText(text);
    
    0 讨论(0)
  • 2020-11-21 18:13

    I was running into the same problem. I could use fromHtml, but I am android now, not web, so I decided to try this out. I do have to localize this though so I gave it a shot using string replacement concept. I set the style on the TextView to be the main style, then just format the other peices.

    I hope this helps others looking to do the same thing - I don't know why this isn't easier in the framework.

    My strings look like this:

    
    <string name="my_text">{0} You will need a {1} to complete this assembly</string>
    <string name="text_sub0">1:</string>
    <string name="text_sub1">screwdriver, hammer, and measuring tape</string>
    

    Here are the styles:

    
    <style name="MainStyle">
        <item name="android:textSize">@dimen/regular_text</item>
        <item name="android:textColor">@color/regular_text</item>
    </style>
    <style name="style0">
        <item name="android:textSize">@dimen/paragraph_bullet</item>
        <item name="android:textColor">@color/standout_text</item>
        <item name="android:textStyle">bold</item>
    </style>
    <style name="style1">
        <item name="android:textColor">@color/standout_light_text</item>
        <item name="android:textStyle">italic</item>
    </style>
    

    Here is my code that calls my formatStyles method:

    
    SpannableString formattedSpan = formatStyles(getString(R.string.my_text), getString(R.string.text_sub0), R.style.style0, getString(R.string.main_text_sub1), R.style.style1);
    textView.setText(formattedSpan, TextView.BufferType.SPANNABLE);
    

    The format method:

    
    private SpannableString formatStyles(String value, String sub0, int style0, String sub1, int style1)
    {
        String tag0 = "{0}";
        int startLocation0 = value.indexOf(tag0);
        value = value.replace(tag0, sub0);
    
        String tag1 = "{1}";
        int startLocation1 = value.indexOf(tag1);
        if (sub1 != null && !sub1.equals(""))
        {
            value = value.replace(tag1, sub1);
        }
    
        SpannableString styledText = new SpannableString(value);
        styledText.setSpan(new TextAppearanceSpan(getActivity(), style0), startLocation0, startLocation0 + sub0.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (sub1 != null && !sub1.equals(""))
        {
            styledText.setSpan(new TextAppearanceSpan(getActivity(), style1), startLocation1, startLocation1 + sub1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    
        return styledText;
    }
    
    0 讨论(0)
  • 2020-11-21 18:14

    Spanny make SpannableString easier to use.

    Spanny spanny = new Spanny("Underline text", new UnderlineSpan())
                    .append("\nRed text", new ForegroundColorSpan(Color.RED))
                    .append("\nPlain text");
    textView.setText(spanny)
    
    0 讨论(0)
  • 2020-11-21 18:14

    As Jon said, for me this is the best solution and you dont need to set any text at runtime, only use this custom class HtmlTextView

    public class HtmlTextView extends TextView {
    
      public HtmlTextView(Context context) {
          super(context);
      }
    
      public HtmlTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
    
      public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) 
      {
          super(context, attrs, defStyleAttr);
      }
    
      @TargetApi(21)
      public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
      }
    
      @Override
      public void setText(CharSequence s,BufferType b){
          super.setText(Html.fromHtml(s.toString()),b);
      }
    
    }
    

    and thats it, now only put it in your XML

    <com.fitc.views.HtmlTextView
        android:id="@+id/html_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/example_html" />
    

    with your Html String

    <string name="example_html">
    <![CDATA[
    <b>Author:</b> Mr Donuthead<br/>
    <b>Contact:</b> me@donut.com<br/>
    <i>Donuts for life </i>
    ]]>
    

    0 讨论(0)
  • 2020-11-21 18:16

    Me Too

    How about using some beautiful markup with Kotlin and Anko -

    import org.jetbrains.anko.*
    override fun onCreate(savedInstanceState: Bundle?) {
        title = "Created with Beautiful Markup"
        super.onCreate(savedInstanceState)
    
        verticalLayout {
            editText {
                hint = buildSpanned {
                    append("Italic, ", Italic)
                    append("highlighted", backgroundColor(0xFFFFFF00.toInt()))
                    append(", Bold", Bold)
                }
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题