按钮的多种字体大小不起作用

Multiple font size for button not working

当我尝试为回收站视图中的按钮文本设置多种字体大小时,它没有按预期工作。

我想做的是获取文本的第一个字母并将其变大一点,添加换行符,然后添加实际文本并将整个文本设置为 button.Here 中的代码 I试过了,但没有按预期工作(大小没有改变)。

public void onBindViewHolder(final PopularCityViewHolder holder, int position) {
    if (shouldShowLoadingView()) return;
    PopularCity x = mItems.get(position);
    stringBuilder = new StringBuilder();
    stringBuilder.append(x.districtName.charAt(0));
    stringBuilder.append("\n\n");
    stringBuilder.append(x.districtName);

    SpannableString spannableString = new SpannableString(stringBuilder.toString());
    spannableString.setSpan(new RelativeSizeSpan(2.0f), 0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    holder.mBtn.getLayoutParams().width = prefWidthAndHeight;
    holder.mBtn.getLayoutParams().height = prefWidthAndHeight;
    String[] colors = colorCodes.get(position).split(",");
    int bg = Color.rgb(Integer.parseInt(colors[0]), Integer.parseInt(colors[1]), Integer.parseInt(colors[2]));
    holder.mBtn.setBackgroundColor(bg);
    holder.mBtn.setText(spannableString);
}

这段代码有什么问题?

试试这个我的朋友

TextView tv= (TextView) findViewById(R.id.tv2);
    String title="Nilesh";

    final SpannableString spannableString = new SpannableString(title);
    int position = 0;
    for (int i = 0, ei = title.length(); i < ei; i++) {
        char c = title.charAt(i);
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
            position = i;
            break;
        }
    }
    spannableString.setSpan(new RelativeSizeSpan(2.0f), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    tv.setText(spannableString, TextView.BufferType.SPANNABLE);