Different textsizes in a button

时光毁灭记忆、已成空白 提交于 2019-12-04 19:35:32

问题


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.

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

回答1:


You can apply AbsoluteSizeSpan in your Spannable to make different size of Text in same content.

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



回答2:


Instead of doing it through code, you may simply achieve it by using html stuff in string resource. 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"


来源:https://stackoverflow.com/questions/12193785/different-textsizes-in-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!