具有不同文本大小的 TextView

发布于 2024-12-04 04:26:43 字数 343 浏览 2 评论 0原文

是否可以在一个 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 呢?

Is this possible to set different textSize in one TextView? I know that I can change text style using:

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

I know the range start...end of text I want to change size. But what can I use as arg0 and arg3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装爱人 2024-12-11 04:26:43

尝试

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

Try

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
油饼 2024-12-11 04:26:43

我知道很晚才回答,但人们仍然可能有同样的问题。即使我也很挣扎
关于这一点。
假设您的 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

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

I know very late to answer but people are still might have the same question.Even I struggled a lot
on this.
Suppose you have these two strings inside your strings.xml file

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

You now need to define two Style for them inside your style.xml with different 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>

Now from your Java file you need to use the spannable to load these two style and strings on single 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);

Below is the formatStyles method which will return the formatted string after applying the style

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;
}
薄荷港 2024-12-11 04:26:43

尝试使用 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();`

Try with AbsoluteSizeSpan

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

The complete code is:

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();`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文