按钮的多种字体大小不起作用 [英] Multiple font size for button not working

查看:118
本文介绍了按钮的多种字体大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在回收站视图中为按钮文本设置多种字体大小时,它无法正常工作.

When I tried to set multiple font sizes for button text inside a recycler view it is not working as expected.

我想做的是获取文本的第一个字母,使其稍大一点,添加换行符,然后输入实际文本,然后在按钮中设置整个文本.这是我尝试过的代码,但无法正常工作(大小未更改).

What I was trying to do is get the first letter of the text and make it a little bigger, add line break and then the actual text and set the whole text in the button.Here is the code I've tried, but is not working as expected (the size is not changing).

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

这篇关于按钮的多种字体大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆