Android implements TextView with different font colors
If you encounter needs, mark it.
Implementation code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Project name: trunk * Class Description: * Created by: Administrator * Created at: 2015/12/10 14:05 * Modified by: Administrator * Modification time: 2015/12/10 14:05 * Remarks for modification: */ public class DifferentColorTextView extends TextView{ public DifferentColorTextView(Context context) { super(context); } public DifferentColorTextView(Context context, AttributeSet attrs) { super(context, attrs); } public DifferentColorTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { (canvas); } public void setDifferentColorText(List<String> text,List<Integer>colors){ setText(calculateResidue(text,colors)); } /**Show different colors*/ private SpannableStringBuilder calculateResidue(List<String> text, List<Integer>colors) { if(text==null||colors==null){ return null; } StringBuilder sb =new StringBuilder(); for (int i=0;i<();i++){ ((i)); } SpannableStringBuilder ssb=new SpannableStringBuilder(()); int begin=0; for (int i=0;i<();i++){ ForegroundColorSpan mSpan = new ForegroundColorSpan((i)); (mSpan,begin,begin+(i).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); begin=begin+(i).length(); } return ssb; } }
Implement the same textView with different colors.
Thank you for reading, I hope it can help you. Thank you for your support for this site!