原文网址: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>

【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色的更多相关文章

  1. 如何设置TextView控件的背景透明度和字体透明度

    如何设置TextView控件的背景透明度和字体透明度 设计师给的标注都是类似这样的: 字号:26 颜色:#000000 透明度:80% 其实,程序上只要需要一个色值就OK了,那么这个色值我如何计算呢? ...

  2. 为什么阿里巴巴Java开发手册中不允许魔法值出现在代码中?

    在阅读<阿里巴巴Java开发手册>时,发现有一条关于关于常量定义的规约,具体内容如下: 图中的反例是将数据缓存起来,并使用魔法值加链路 id 组成 key,这就可能会出现其他开发人员在复制 ...

  3. VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]

    在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...

  4. 设置TextView控件的背景透明度和字体透明度

    TextView tv = (TextView) findViewById(R.id.xx); 第1种:tv.setBackgroundColor(Color.argb(255, 0, 255, 0) ...

  5. Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵

    我们如今准备好在代码中加入透视投影了. Android的Matrix类为它准备了两个方法------frustumM()和perspectiveM(). 不幸的是.frustumM()的个缺陷,它会影 ...

  6. IDEA中的替换功能(替换代码中的变量名很好用哦)

    刚刚上班不久,这两天正在研究公司项目里面的代码,今天用阿里的插件扫描了一下代码,发现代码中有很多变量的命名,没有遵循驼峰式的命名规则.一开始我一个一个的修改这些变量名,后来无意中用了一下Ctrl+F( ...

  7. 使用Java8中的Optional类来消除代码中的null检查

    简介 Optional类是Java 8新增的一个类,Optional 类主要解决的问题是臭名昭著的空指针异常(NullPointerException). —— 每个 Java 程序员都非常了解的异常 ...

  8. Android 之px于dp在Java代码中的转换

    现在由于用到了,使用代码进行动态布局,所以需要进行px于dp之间的转换. 现将其封装为方法,以便于调用. public int DpToPx(Context context,float dp){ fl ...

  9. iOS导航栏NavigationBar的颜色,按钮和标题以及字体颜色

    首先,层级关系: leftBarButtonItem.rightBarButtonItem.title都是加在UINavigationItem上的,UINavigationItem再加在Navigat ...

  10. sublime中使用插件anaconda而在代码中出现方框

    这个标志是说不符合PEP8标准,比如使用了Tab做缩进:一行过长等问题. 可以在可以在 Sublime > Preferences > Package Settings > Anac ...

随机推荐

  1. Solr配置与简单Demo

    简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/solr/  .solr需要运行在 ...

  2. swift-08-元组分解和数组

    //1.有时候需要把元组中的数据拆分出来使用比如: var stu = ("范冰冰",30,"女") // 1)将stu中的数据赋值给三个变量. var (na ...

  3. LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)

    Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...

  4. 自己动手丰衣足食,h5手机端jquery弹窗插件(事件冒泡、单例模式、遮盖部分禁止默认滚动)

    感谢浏览,欢迎交流=.= 公司开发微信网页多处需要使用弹窗,使用jquery-ui的定制化下载仍需要150多kb,想来有些奢侈(最终下来只有11kb,压缩后2kb,啊,我的神), 手机端弹窗方式与pc ...

  5. PHP 文字,图片水印,缩略图,裁切成小图(大小变小)

    文字水印基本思路:1.用getimagesize()获取图片的信息(as:大小,属性等):2.根据图片信息用imagecreatefromjpeg ()/imagecreatefromgif/imag ...

  6. DevExpress控件-- Gridcontrol合并表头

    写在前面的话: 在园子里逛了有一段时间了,一直想写点东西,但苦于自己的水平有限,生怕写出来的东西浪费了读者的时间.楼主有幸参加了公司DevExpress控件的培训,独乐乐不如众乐乐,特附上Demo以飨 ...

  7. 给destoon商城的列表中和首页添加购物车功能

    如何给destoon商城的列表中和首页添加购物车功能? 目前加入购物车的功能只存在商城的详细页面里,有时候我们需要批量购买的时候,希望在列表页就能够使用这个加入购物车的功能. 修改步骤见下: 例如在商 ...

  8. Oracle安装时忘记解锁scott用户的解决方案

    最近笔者开始学习Oracle,对于笔者同学安装过程出现各种问题而导致重做系统表示默哀. (1)问题1:64位操作系统安装完以后选择java.exe文件后.sqldeveloper闪退,原因是64位操作 ...

  9. maya2105 - windows8 - numpy/scipy

    To compile numpy, create a site.cfg file in numpy's source directory with the following or similar c ...

  10. Xcode8和iOS10的适配问题

    本文转自:http://www.jianshu.com/p/90d5323cf510 =================== 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功 ...