zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }

    效果:

  • 相关阅读:
    Redhat7 安装 yum源(亲测有效)
    Win10开启FTP与配置(完整无错版)
    DataStage中Transformer的函数大全
    缺省值是什么
    扫描线
    2021.07.02笔记-DP
    2021.07.02-2膜你赛
    2021.07.01膜你赛
    2021.06.21模拟赛
    2021.06.09模拟赛
  • 原文地址:https://www.cnblogs.com/fightzhao/p/4900641.html
Copyright © 2011-2022 走看看