Android TextView 设置文字背景色或文字颜色

       String str= "这是设置TextView部分文字背景颜色和前景颜色的demo!" ;
         int  bstart=str.indexOf( "背景" );
         int  bend=bstart+ "背景" .length();
         int  fstart=str.indexOf( "前景" );
         int  fend=fstart+ "前景" .length();
         SpannableStringBuilder style= new  SpannableStringBuilder(str);
         style.setSpan( new  BackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
         style.setSpan( new  ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         TextView tvColor=(TextView) findViewById(R.id.tv_color);
         tvColor.setText(style);

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/80966493