android中SpannableString的使用

1,简单使用http://blog.csdn.net/harvic880925/article/details/38984705

 

2,例子:

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.tv);
        //改变字体颜色
        //先构造SpannableString
        //将这个Span应用于指定范围的字体
        tv.setText(spanString(20.085));
    }

    private  SpannableString spanString(double price) {
        SpannableString spannableString = new SpannableString(getApplication().getString(R.string.dg_ticket_yuan,
                Units.wrapPrice(price)));
        int priceLength = spannableString.length() - 1;
        //再构造一个改变字体颜色的Span
        spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //再构造一个改变字体大小的Span
        spannableString.setSpan(new AbsoluteSizeSpan(25, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(50, true), 1, priceLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(25, true), priceLength, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}

效果:

posted @ 2015-10-22 13:30  fightzhao  阅读(2251)  评论(0编辑  收藏  举报