简体   繁体   English

不同的文本按钮

[英]Different textsizes in a button

I want to create a button. 我想创建一个按钮。 This button contains a letter with a size of 22pixels and some letters to the right of it with a size of 16 pixels. 此按钮包含一个大小为22像素的字母和右侧的一些字母,大小为16像素。

Like this: 像这样:
在此输入图像描述

How can I achieve this? 我怎样才能做到这一点?

At the moment I have this: 目前我有这个:

private void setText(Button btn, String text, String underText) {
    Spannable span = new SpannableString(text + "\n" +  underText);

    btn.setText(span);
}

You can apply AbsoluteSizeSpan in your Spannable to make different size of Text in same content. 您可以在Spannable中应用AbsoluteSizeSpan ,以在相同内容中创建不同大小的Text。

 private void setText(Button btn, String text, String underText) {

    Spannable span = new SpannableString(text + "\n" +  underText);
    span.setSpan(new AbsoluteSizeSpan(fontSize), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    btn.setText(span);
}

Instead of doing it through code, you may simply achieve it by using html stuff in string resource . 您可以通过在字符串资源中使用html来实现它,而不是通过代码完成它。 For example: 例如:

Define a string resource: 定义字符串资源:

<string name="tmp"><font size="30">2</font>abc</string>

and apply it to your button's text: 并将其应用于按钮的文本:

android:text="@string/tmp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM