android textview改變部分文字的顏色和string.xml中文字的替換及部分內容設置顏色、字體、超鏈接、圖片


一:TextView組件改變部分文字的顏色:

1.TextView textView = (TextView)findViewById(R.id.textview);  
2.  
3.//方法一:  
4.textView.setText(Html.fromHtml("<font color=\"#ff0000\">紅色</font>其它顏色"));  
5.  
6.//方法二:  
7. String text = "獲得銀寶箱!";  
8. SpannableStringBuilder style=new SpannableStringBuilder(text);     
9.  style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //設置指定位置textview的背景顏色  
10.  style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //設置指定位置文字的顏色  
11.  textView.setText(style);  

二:android string.xml文件中的整型和string型代替:

String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"銀寶箱");  

對應的string.xml文件參數:

<string name="baoxiang">您今天打了%1$d局,還差%2$d局可獲得%3$s!</string>  
%1$d表達的意思是整個name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一個整型%1$d表達的意思是整個name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一個整型

在項目開發者,經常需要把以上兩者結合起來使用。可以避免很多textview的拼接,如下所示:

1.TextView textView = (TextView)findViewById(R.id.testview);  
2.  
3.String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"銀寶箱");  
4.       int index[] = new int[3];  
5.       index[0] = text.indexOf("2");  
6.       index[1] = text.indexOf("18");  
7.       index[2] = text.indexOf("銀寶箱");  
8.  
9. SpannableStringBuilder style=new SpannableStringBuilder(text);     
10.           style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
11.           style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
12.           style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
13.           textView.setText(style);

 

 //android TextView、EditText對部分內容設置顏色、字體、超鏈接、圖片; 
//這里是以一個TextView為例子,EditText的設置方法和TextView一樣

//TextView對象
TextView txtInfo = new TextView(this);

//文本內容
SpannableString ss = new SpannableString("紅色打電話斜體刪除線綠色下划線圖片:.");

//設置0-2的字符顏色
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//設置2-5的字符鏈接到電話簿,點擊時觸發撥號
ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//設置9-11的字符為網絡鏈接,點擊時打開頁面
ss.setSpan(new URLSpan("http://www.hao123.com"), 9, 11,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//設置13-15的字符點擊時,轉到寫短信的界面,發送對象為10086
ss.setSpan(new URLSpan("sms:10086"), 13, 15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//粗體
 ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//斜體
ss.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//下划線
ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//以下代碼是在指定位置插入圖片
Drawable d = getResources().getDrawable(R.drawable.icon);

//設置圖片大小
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

//插入的位置
ss.setSpan(new ImageSpan(d, ImageSpan.ALIGN_BASELINE), 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

//設置文本內容到textView
txtInfo.setText(ss);

//不添加這一句,撥號,http,發短信的超鏈接不能執行.
txtInfo.setMovementMethod(LinkMovementMethod.getInstance());

 

 
 


免責聲明!

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



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