1. 程式人生 > >設定textview字型顏色的幾種方法

設定textview字型顏色的幾種方法

轉自:http://txlong-onz.iteye.com/blog/1249609

android中設定TextView的顏色有方法setTextColor,這個方法被過載了,可以傳入兩種引數。

Java程式碼  收藏程式碼
  1. public void setTextColor(int color) {  
  2.     mTextColor = ColorStateList.valueOf(color);  
  3.     updateTextColors();  
  4. }  
  5. public void setTextColor(ColorStateList colors) {  
  6.     if (colors == null
    ) {  
  7.         throw new NullPointerException();  
  8.     }  
  9.     mTextColor = colors;  
  10.     updateTextColors();  
  11. }  

下邊就分別寫出怎麼使用這兩個方法設定TextView的顏色:

Java程式碼  收藏程式碼
  1. TextView tv = new TextView(this);  
  2. tv.setText("Test set TextView's color.");  
  3. //方案一:程式碼中通過argb值的方式  
  4. tv.setTextColor(Color.rgb(255255255
    ));  

這種方法也就是傳入int color值,這個int不是R檔案中自動分配的int值,所以要注意。這是Color類中的靜態方法構造出來的顏色int值。

Java程式碼  收藏程式碼
  1. Resources resource = (Resources) getBaseContext().getResources();  
  2. ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);  
  3. if (csl != null) {  
  4.     tv.setTextColor(csl);  
  5. }  

這種方法是通過ColorStateList得到xml中的配置的顏色的。好多需要xml中配置的都要類似這樣的對映xml檔案。

還有種方法:

Java程式碼  收藏程式碼
  1. XmlResourceParser xrp = getResources().getXml(R.color.my_color);  
  2. try {  
  3.     ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);  
  4.     tv.setTextColor(csl);  
  5. catch (Exception e) {  
  6. }  

 全部程式碼:

Java程式碼  收藏程式碼
  1. package com.txlong;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6. public class ListViewDemoActivity extends Activity {  
  7.     // private ListView listView;  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         TextView tv = new TextView(this);  
  13.         tv.setText("Test set TextView's color.");  
  14.         //方案一:通過ARGB值的方式  
  15.         /** 
  16.          * set the TextView color as the 0~255's ARGB,These component values 
  17.          * should be [0..255], but there is no range check performed, so if they 
  18.          * are out of range, the returned color is undefined 
  19.          */  
  20. //      tv.setTextColor(Color.rgb(255, 255, 255));  
  21.         /** 
  22.          * set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue', 
  23.          * 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 
  24.          * 'lightgray', 'darkgray' 
  25.          */  
  26.         tv.setTextColor(Color.parseColor("#FFFFFF"));  
  27.         /** 原來不知道有上邊的方法,就用這個笨笨方法了 */  
  28. //      String StrColor = null;  
  29. //      StrColor = "FFFFFFFF";  
  30. //      int length = StrColor.length();  
  31. //      if (length == 6) {  
  32. //          tv.setTextColor(Color.rgb(  
  33. //                  Integer.valueOf(StrColor.substring(0, 2), 16),  
  34. //                  Integer.valueOf(StrColor.substring(2, 4), 16),  
  35. //                  Integer.valueOf(StrColor.substring(4, 6), 16)));  
  36. //      } else if (length == 8) {  
  37. //          tv.setTextColor(Color.argb(  
  38. //                  Integer.valueOf(StrColor.substring(0, 2), 16),  
  39. //                  Integer.valueOf(StrColor.substring(2, 4), 16),  
  40. //                  Integer.valueOf(StrColor.substring(4, 6), 16),  
  41. //                  Integer.valueOf(StrColor.substring(6, 8), 16)));  
  42. //      }  
  43.         //方案二:通過資源引用  
  44. //      tv.setTextColor(getResources().getColor(R.color.my_color));  
  45.         //方案三:通過資原始檔寫在String.xml中  
  46. //      Resources resource = (Resources) getBaseContext().getResources();  
  47. //      ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);  
  48. //      if (csl != null) {  
  49. //          tv.setTextColor(csl);  
  50. //      }  
  51.         //方案四:通過xml檔案,如/res/text_color.xml  
  52. //      XmlPullParser xrp = getResources().getXml(R.color.text_color);  
  53. //      try {  
  54. //          ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);  
  55. //          tv.setTextColor(csl);  
  56. //      } catch (Exception e) {  
  57. //      }  
  58.         // listView = new ListView(this);  
  59.         //  
  60.         // Cursor cursor = getContentResolver().query(  
  61.         // Uri.parse("content://contacts/people"), null, null, null, null);  
  62.         //  
  63.         // startManagingCursor(cursor);  
  64.         //  
  65.         // ListAdapter listAdapter = new SimpleCursorAdapter(this,  
  66.         // android.R.layout.simple_expandable_list_item_2, cursor,  
  67.         // new String[] { "name", "name" }, new int[] {  
  68.         // android.R.id.text1, android.R.id.text2 });  
  69.         //  
  70.         // listView.setAdapter(listAdapter);  
  71.         // setContentView(listView);  
  72.         setContentView(tv);  
  73.     }  
  74. }  

String.xml檔案為:

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, ListViewDemoActivity!</string>  
  4.     <string name="app_name">ListViewDemo</string>  
  5.     <color name="my_color">#FFFFFF</color>  
  6. </resources>  

/res/color/text_color.xml

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true" android:color="#FF111111"/>  
  4.     <!-- pressed -->  
  5.     <item android:state_focused="true" android:color="#FF222222"/>  
  6.     <!-- focused -->  
  7.     <item android:state_selected="true" android:color="#FF333333"/>  
  8.     <!-- selected -->  
  9.     <item android:state_active="true" android:color="#FF444444"/>  
  10.     <!-- active -->  
  11.     <item android:state_checkable="true" android:color="#FF555555"/>  
  12.     <!-- checkable -->  
  13.     <item android:state_checked="true" android:color="#FF666666"/>  
  14.     <!-- checked -->  
  15.     <item android:state_enabled="true" android:color="#FF777777"/>  
  16.     <!-- enabled -->  
  17.     <item android:state_window_focused="true" android:color="#FF888888"/>  
  18.     <!-- window_focused -->  
  19. </selector>