【问题标题】:Custom sized italic Font using Spannable使用 Spannable 自定义大小的斜体字体
【发布时间】:2013-09-04 18:57:50
【问题描述】:

有如下代码:

String s = message + "\n" + date;
Spannable f = Spannable.Factory.getInstance().newSpannable(s);
f.setSpan(new ForegroundColorSpan(Color.BLUE), 0, message.length(), 0);
f.setSpan(new ForegroundColorSpan(Color.RED), message.length() + 1, s.length(), 0);

textView.setText(f);

此代码适用于为 textView 设置不同的颜色。但我需要另一个功能 - 我希望“日期”的字体和斜体字号更小。我怎样才能做到这一点?

【问题讨论】:

    标签: java android spannable


    【解决方案1】:

    检查一下

    Different font size of strings in the same TextView

    试试下面的

    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);
    

    更多样式

    http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring

    【讨论】:

      【解决方案2】:

      你可以使用 Html.fromHtml("your html string"),在 HTML 中做任何你想要的样式,然后在 textview 中显示,如下所示:

       Spanned htmlText = Html.fromHtml(getString(R.string.yourStringResource));
       yourTextView.setText(htmlText, TextView.BufferType.SPANNABLE);
      

      而XML string.xml,必须有如下声明的字符串:

       <string name="yourStringResource"><![CDATA[BLA BLA BLA BLA <br/> BLABA <font color="blue" size="6">COLORED HTML</font>]]></string>
      

      希望这会有所帮助。

      问候!

      【讨论】:

        【解决方案3】:

        您可以使用 HTML 标签,如下所示:

        String s = "<html>" + message + "<br /><i><small>" + date + "</i></small></html>";
        

        【讨论】:

          【解决方案4】:

          是的,这绝对有可能。您可以简单地添加另一个 Span

          Spannable.setSpan(new StyleSpan(Typeface.ITALIC), from, to, 0);
          Spannable.setSpan(new RelativeSizeSpan(float size), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          

          在这种情况下,它是 StyleSpan斜体RelativeSizeSpan

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-30
            • 1970-01-01
            • 2020-02-29
            • 2013-01-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-15
            相关资源
            最近更新 更多