【问题标题】:TextView with different textSize具有不同 textSize 的 TextView
【发布时间】:2011-09-12 13:08:29
【问题描述】:

这可以在一个 TextView 中设置不同的 textSize 吗?我知道我可以使用以下方法更改文本样式:

TextView textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(textView.getText());
span.setSpan(arg0, 1, 10, arg3);
textView.setText(span)

我知道要更改大小的文本范围开始...结束。但是arg0arg3 可以使用什么?

【问题讨论】:

    标签: android textview spannable


    【解决方案1】:

    试试 AbsoluteSizeSpan

    snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    完整代码为:

    SpannableStringBuilder snackbarText = new SpannableStringBuilder();
    snackbarText.append("Your text");
    snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    Snackbar.make(getCurrentFocus(), snackbarText, Snackbar.LENGTH_LONG).setAction("Action", null).show();`
    

    【讨论】:

      【解决方案2】:

      试试

      span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      

      【讨论】:

      • 它看起来正常但不起作用。我的 TextView 中的整个文本始终具有相同的大小。
      • 嗯,也许我应该在 setSpan() 之后将 span 应用(更新)到我的 TextView?
      • 对我的 3 个 cmets 感到抱歉,但我已经解决了这个问题。只是:textView.setText(span).
      • 你知道如何将尺寸和颜色结合起来以获得 span 吗?
      【解决方案3】:

      我知道很晚才能回答,但人们仍然可能有同样的问题。即使我也很挣扎 对此。 假设您的 strings.xml 文件中有这两个字符串

       <string name="my_text">You will need a to complete this assembly</string>
       <string name="text_sub1">screwdriver, hammer, and measuring tape</string>
      

      您现在需要在 style.xml 中为它们定义两个具有不同 textSize 的样式

      <style name="style0">
          <item name="android:textSize">19sp</item>
          <item name="android:textColor">@color/standout_text</item>
          <item name="android:textStyle">bold</item>
      </style>
      <style name="style1">
          <item name="android:textSize">23sp</item>
          <item name="android:textColor">@color/standout_light_text</item>
          <item name="android:textStyle">italic</item>
      </style>
      

      现在从你的 Java 文件中,你需要使用 spannable 在单个 textView 上加载这两种样式和字符串

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

      下面是formatStyles方法,它会在应用样式后返回格式化字符串

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-13
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多