`

Android中设置TextView的颜色setTextColor

 
阅读更多

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

 

public void setTextColor(int color) {
    mTextColor = ColorStateList.valueOf(color);
    updateTextColors();
}

public void setTextColor(ColorStateList colors) {
    if (colors == null) {
        throw new NullPointerException();
    }

    mTextColor = colors;
    updateTextColors();
}

 

下边就分别写出怎么使用这两个方法设置TextView的颜色:

 

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

 

这种方法也就是传入int color值,这个int不是R文件中自动分配的int值,所以要注意。这是Color类中的静态方法构造出来的颜色int值。

 

Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
if (csl != null) {
	tv.setTextColor(csl);
}

 

这种方法是通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件。

还有种方法:

 

XmlResourceParser xrp = getResources().getXml(R.color.my_color);
try {
	ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
	tv.setTextColor(csl);
} catch (Exception e) {
}

 全部代码:

 

package com.txlong;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class ListViewDemoActivity extends Activity {
	// private ListView listView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TextView tv = new TextView(this);
		tv.setText("Test set TextView's color.");
		//方案一:通过ARGB值的方式
		/**
		 * set the TextView color as the 0~255's ARGB,These component values
		 * should be [0..255], but there is no range check performed, so if they
		 * are out of range, the returned color is undefined
		 */
//		tv.setTextColor(Color.rgb(255, 255, 255));
		/**
		 * set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',
		 * 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',
		 * 'lightgray', 'darkgray'
		 */
		tv.setTextColor(Color.parseColor("#FFFFFF"));
		
		
		/** 原来不知道有上边的方法,就用这个笨笨方法了 */
//		String StrColor = null;
//		StrColor = "FFFFFFFF";
//		int length = StrColor.length();
//		if (length == 6) {
//			tv.setTextColor(Color.rgb(
//					Integer.valueOf(StrColor.substring(0, 2), 16),
//					Integer.valueOf(StrColor.substring(2, 4), 16),
//					Integer.valueOf(StrColor.substring(4, 6), 16)));
//		} else if (length == 8) {
//			tv.setTextColor(Color.argb(
//					Integer.valueOf(StrColor.substring(0, 2), 16),
//					Integer.valueOf(StrColor.substring(2, 4), 16),
//					Integer.valueOf(StrColor.substring(4, 6), 16),
//					Integer.valueOf(StrColor.substring(6, 8), 16)));
//		}
		
		//方案二:通过资源引用
//		tv.setTextColor(getResources().getColor(R.color.my_color));
		
		//方案三:通过资源文件写在String.xml中
//		Resources resource = (Resources) getBaseContext().getResources();
//		ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
//		if (csl != null) {
//			tv.setTextColor(csl);
//		}

		//方案四:通过xml文件,如/res/text_color.xml
//		XmlPullParser xrp = getResources().getXml(R.color.text_color);
//		try {
//			ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
//			tv.setTextColor(csl);
//		} catch (Exception e) {
//		}
		
		// listView = new ListView(this);
		//
		// Cursor cursor = getContentResolver().query(
		// Uri.parse("content://contacts/people"), null, null, null, null);
		//
		// startManagingCursor(cursor);
		//
		// ListAdapter listAdapter = new SimpleCursorAdapter(this,
		// android.R.layout.simple_expandable_list_item_2, cursor,
		// new String[] { "name", "name" }, new int[] {
		// android.R.id.text1, android.R.id.text2 });
		//
		// listView.setAdapter(listAdapter);
		// setContentView(listView);
		setContentView(tv);
	}
}

String.xml文件为:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, ListViewDemoActivity!</string>
    <string name="app_name">ListViewDemo</string>

    <color name="my_color">#FFFFFF</color>

</resources>

/res/color/text_color.xml

 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="#FF111111"/>
    <!-- pressed -->
    <item android:state_focused="true" android:color="#FF222222"/>
    <!-- focused -->
    <item android:state_selected="true" android:color="#FF333333"/>
    <!-- selected -->
    <item android:state_active="true" android:color="#FF444444"/>
    <!-- active -->
    <item android:state_checkable="true" android:color="#FF555555"/>
    <!-- checkable -->
    <item android:state_checked="true" android:color="#FF666666"/>
    <!-- checked -->
    <item android:state_enabled="true" android:color="#FF777777"/>
    <!-- enabled -->
    <item android:state_window_focused="true" android:color="#FF888888"/>
    <!-- window_focused -->

</selector>

 

 

分享到:
评论
4 楼 diyangxia 2014-04-25  
tv.setTextColor(Color.parseColor("FFFFFF")); 
3 楼 txlong_onz 2013-10-05  
dujiaju 写道
  楼主!!!!!!

        tv.setTextColor(Color.parseColor("FFFFFF"));  

一定要加#号 啊!!!!!!!
        tv.setTextColor(Color.parseColor("#FFFFFF"));  

我调试了半天!!!!!!!!!!

不好意思,没有检查,多谢指正,已经修改。
2 楼 liyong704 2013-09-28  
 
1 楼 dujiaju 2013-02-18  
  楼主!!!!!!

        tv.setTextColor(Color.parseColor("FFFFFF"));  

一定要加#号 啊!!!!!!!
        tv.setTextColor(Color.parseColor("#FFFFFF"));  

我调试了半天!!!!!!!!!!

相关推荐

    Android编程设置TextView颜色setTextColor用法实例

    本文实例讲述了Android编程设置TextView颜色setTextColor用法。分享给大家供大家参考,具体如下: android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数。 public void ...

    Android中设置TextView的颜色setTextColor两种方法

    android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数。参考博客:http://blog.csdn.net/u010963246/article/details/47399859

    Android编程实现TextView字体颜色设置的方法小结

    本文实例讲述了Android编程实现TextView字体颜色设置的方法。分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值。例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法...

    Android 即时添加textview、imageview内容项.rar

    动态生成每个下拉项对应的View,每个下拉项View由LinearLayout中包含一个ImageView及一个TextView构成,然后为ListView设置内容适配器和设置选项被单击的监听器。以下代码或许对你有帮助:  LinearLayout ll=new ...

    Android用户界面开发之:TextView的使用实例

    TextView就是一个用来显示文本标签的控件。 代码如下:/*TextView的设置*//*... /* 设置文本的颜色 */ textview.setTextColor(Color.RED); /* 设置字体大小 */ textview.setTextSize(20); /* 设置文字背景 */ tex

    浅析Android TextView常用属性

    以下介绍的是XML代码中的属性,在java代码中同样可通过 ”组件名.setXXX()方法设置。如,tv.setTextColor();  【属性一】 android:textColor=#000 表示文字的颜色。 【提示】颜色可以随便写一个“#000”形式的...

    SuperTextView-从未如此惊艳!一个超级的TextView.zip

     //注意,启用这个模式之后通过setTextColor()设置的颜色将会被覆盖。  //你需要通过text_fill_color来设置文字的颜色。  app:text_stroke="true"   // 文字的描边颜色。默认为Color.BLACK。  app:text_...

    高仿微信界面

    这个的切换其实就是切换准备好的png图片和改变文字的颜色。 下面是刚才导入的底部导航栏xml文件: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android=...

    星座说明书

     Android的每一个可视化界面,都有其唯一的布局配置文件,该文件里面有各种布局方式,各种资源文件如图像,文字,颜色的引用,程序在运行时,可以通过代码对配置文件进行读取。这样就可以形成不同的可视化界面和...

    SuperButton-这真的可能是最好用的按钮了.zip

    大概分为以下几个步骤:在xml布局里面按照设计稿的尺寸位置写一个Textview按照设计稿规定的颜色和圆角在drawable目录下创建一个shape文件将这个shape文件作为Textview的背景这样一个很标准的按钮就诞生了,然后就...

Global site tag (gtag.js) - Google Analytics