TextView使用不同的textSize

woy*_*aru 82 android textview spannable

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

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

我知道范围开始...文本结束我想改变大小.但我能为使用arg0arg3

Mic*_*ele 187

尝试

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

  • 对不起我的3条评论,但我已经解决了这个问题.只是:`textView.setText(span)`. (4认同)

Joh*_*ohn 20

我知道很晚才回答,但人们仍然会有同样的问题.即使我在这方面做了很多努力.假设在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>
Run Code Online (Sandbox Code Playgroud)

您现在需要在style.xml中使用不同的textSize为它们定义两个Style

<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>
Run Code Online (Sandbox Code Playgroud)

现在,从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);
Run Code Online (Sandbox Code Playgroud)

下面是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;
}
Run Code Online (Sandbox Code Playgroud)


小智 10

尝试使用AbsoluteSizeSpan

snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

完整的代码是:

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();`
Run Code Online (Sandbox Code Playgroud)