【转】Android中设置TextView的颜色setTextColor

原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html

android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数。

  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的颜色:

  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. 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文件为:

  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

  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>
时间: 2024-08-05 11:15:11

【转】Android中设置TextView的颜色setTextColor的相关文章

[转]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

Android中设置TextView的颜色setTextColor

tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColor(Color.rgb(255, 255, 255));  //注意Color是大写C,不是color.holo_orange_dark,这样错误并没效果的 tv.setBackgroundResource(R.drawable.icon_bg_rectang_stroke); 这种方法也就是传入int co

android中用Spannable在TextView中设置超链接、颜色、字体

昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类.首先,看一下其运行效果:  要给 TextView 加上效果,方式主要有几种: 第一种,自动应用效果,使用 android:autolink 属性,如: Java代码   <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1"

Android中设置文本颜色的三种方法及颜色大全

原文:Android中设置文本颜色的三种方法及颜色大全 源代码下载地址:http://www.zuidaima.com/share/1550463694572544.htm 1.利于系统自带的颜色类 如TextView1.setTextColor(Android.graphics.Color.RED); 2.数字颜色表示法 TextView1.setTextColor(0xffff00ff); 3.自定义颜色 TextView1.setTextColor(this.getResources().

如何在Android中为TextView动态设置drawableLeft等

如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 1 Drawable drawable = getResources().getDrawable(resId); 2 //注意查看方法TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) 3 //的注释,要求设置的drawable必须已经通过Drawable.setBounds方法设置过边界参数 4

【原创】如何在Android中为TextView动态设置drawableLeft等

如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 1 Drawable drawable = getResources().getDrawable(resId); 2 //注意查看方法TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) 3 //的注释,要求设置的drawable必须已经通过Drawable.setBounds方法设置过边界参数 4

Android中string.xml文件中设置部分字体颜色大小

1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font color="#7700ff00"><b>%1$d<br/></b></font>清理内存:<font color="#7700ff00"><b>%2$sMB</b></font&g

Android如何设置TextView的行间距、行高。

     Android系统中TextView默认行间距比较窄,不美观.     我们可以设置每行的行间距,可以通过属性android:lineSpacingExtra或android:lineSpacingMultiplier来做. 在你要设置的TextView中加入如下代码: 1.android:lineSpacingExtra 设置行间距,如"8dp". 2.android:lineSpacingMultiplier 设置行间距的倍数,如"1.5″.       示例:

如何在xml中设置textview不可见

可见(visible)XML文件:android:visibility="visible"Java代码:view.setVisibility(View.VISIBLE);不可见(invisible)XML文件:android:visibility="invisible"Java代码:view.setVisibility(View.INVISIBLE);隐藏(GONE)XML文件:android:visibility="gone"Java代码:vi