android settextcolor 引用xml,[转]Android中设置TextView的颜色setTextColor

1 packagecom.txlong;2

3 importandroid.app.Activity;4 importandroid.graphics.Color;5 importandroid.os.Bundle;6 importandroid.widget.TextView;7

8 public class ListViewDemoActivity extendsActivity {9 //private ListView listView;

10

11 /**Called when the activity is first created.*/

12 @Override13 public voidonCreate(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 values21 * should be [0..255], but there is no range check performed, so if they22 * are out of range, the returned color is undefined23 */

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.xml61 //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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值