android--設定TextView部分文字的顏色和背景(高亮顯示)
設定部分文字背景高亮顯示:
顯示效果:
同時設定文字和背景高亮顯示:
顯示效果:
引數說明:
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
API裡面解釋:Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
即在原文字頭或尾追加新文字的樣式不受原文字樣式影響,原文字高亮,新追加文字不高亮。
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
API裡面解釋:Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their ending point but not at their starting point. When 0-length, they behave like points.
即在原文字尾追加新文字的樣式受原文字樣式影響,原來文字尾高亮,新追加文字也高亮。
- public class HighLightActivity extends Activity {
- String strs="我的心太亂了,給我點空白。";
- TextView textview;
- int start =3;
- int end = 5;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.high_light);
- textview=(TextView)findViewById(R.id.textview);
- SpannableStringBuilder style=new SpannableStringBuilder(strs);
- style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- textview.setText(style);
- }
- }
顯示效果:
同時設定文字和背景高亮顯示:
- package com.justel.contact;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableStringBuilder;
- import android.text.style.BackgroundColorSpan;
- import android.text.style.ForegroundColorSpan;
- import android.widget.TextView;
- public class HighLightActivity extends Activity {
- String strs="我的心太亂了,給我點空白。";
- TextView textview;
- int start =3;
- int end = 5;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.high_light);
- textview=(TextView)findViewById(R.id.textview);
- SpannableStringBuilder style=new SpannableStringBuilder(strs);
- style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- textview.setText(style);
- }
- }
顯示效果:
引數說明:
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
API裡面解釋:Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
即在原文字頭或尾追加新文字的樣式不受原文字樣式影響,原文字高亮,新追加文字不高亮。
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
API裡面解釋:Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their ending point but not at their starting point. When 0-length, they behave like points.
即在原文字尾追加新文字的樣式受原文字樣式影響,原來文字尾高亮,新追加文字也高亮。
相關文章
- app直播原始碼,TextView部分字型顏色高亮APP原始碼TextView
- 設定toast的字型顏色和背景顏色AST
- svg 圖示設定背景顏色SVG
- Android開發筆記——TextView文字設定不同顏色Android筆記TextView
- CSS設定選中網頁文字時的背景和顏色CSS網頁
- markdown字型顏色和背景設定
- 直播帶貨系統原始碼利用TextView設定部分字型的顏色和大小原始碼TextView
- Markdown(入門)——文字設定 ->(字型、字號、顏色和背景色)
- 如何設定小程式頁面各個部分的背景顏色?
- CSS設定元素的背景顏色CSS
- qt 設定QTextEdit文字框中指定內容塊的背景顏色QT
- bootstrap datepicker 單獨設定某些日期的特殊背景顏色和某些月份特殊背景boot
- 聊天平臺原始碼,TextView部分文字變色原始碼TextView
- win10系統如何設定Word背景顏色_win10 word頁面背景顏色設定步驟Win10
- CSS 設定文字框游標顏色CSS
- ReSharper 顯示使用的顏色
- CSS設定滑鼠選中文字的顏色CSS
- react native拖動上方顯示值,改變背景顏色的sliderReact NativeIDE
- CSS設定一個文字兩種顏色CSS
- 神奇的 CSS,讓文字智慧適配背景顏色CSS
- 讓你的文字自動適配背景顏色
- CSS 中的顏色、背景和剪下CSS
- typora編寫md檔案文字設定顏色
- JavaScript點選設定背景顏色的選項卡程式碼JavaScript
- js設定頁面TR 的屬性 背景顏色 樣式JS
- Android入門教程 | Button,TextView背景設定AndroidTextView
- CSS 設定字型顏色和大小CSS
- 強大的CSS:顏色、背景和剪下CSS
- linux中顯示有顏色的字元......Linux字元
- 修改VS的程式碼高亮顏色
- 調整Aplayer的歌詞顏色和字型大小顯示
- AUTOCAD——設定顏色
- Android的標題欄,狀態列圖示文字顏色及背景動態變化Android
- win10 如何設定txt背景綠色 win10如何在txt文件修改背景顏色Win10
- 短視訊平臺原始碼,Android中 TextView設定顏色無效的問題原始碼AndroidTextView
- 我的SCRT顏色設定
- Python 查詢PDF中的指定文字並高亮顯示Python
- UE富文字框RichTextBlock的內容設定不同的字型顏色BloC
- JavaScript WebGL 設定顏色JavaScriptWeb