[轉]Android中設置TextView的顏色setTextColor


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

Android中設置TextView的顏色setTextColor

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

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

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

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

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

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文件。

還有種方法:

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 }

 全部代碼:

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

String.xml文件為:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 
4     <string name="hello">Hello World, ListViewDemoActivity!</string>
5     <string name="app_name">ListViewDemo</string>
6 
7     <color name="my_color">#FFFFFF</color>
8 
9 </resources>

/res/color/text_color.xml

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

 


免責聲明!

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



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