android-修改TextView中部分文字的顏色


		textView = (TextView) findViewById(R.id.textview);
		SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());
		
		//ForegroundColorSpan 為文字前景色,BackgroundColorSpan為文字背景色
		ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
		ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
		ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);
		ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);
		ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);
		


		builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		textView.setText(builder);

  或者 采用 html 的方式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM