`

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颜色的技术。 至于“TestColor”这个文件名,可能是测试颜色变化的一个例子或者包含测试用例的代码文件。如果能查看到这个文件的内容...

    安卓textView相关-代码动态改变某些textview文本颜色及其大小的方式.rar

    在Android中,我们可以使用`setTextColor()`方法来改变TextView的文本颜色。这个方法接受一个颜色值或颜色资源ID作为参数。例如,如果你想设置文本为红色,可以这样写: ```java textView.setTextColor(Color....

    Android代码-textView应用简单示例.zip

    综上所述,这个“Android代码-textView应用简单示例.zip”压缩包可能包含的就是如何在XML布局文件中定义TextView,如何在Java代码中操作TextView,以及如何设置和使用TextView的多种属性的实例。通过学习这些基本...

    Android Textview颜色并可以点击

    在XML布局文件中,我们可以直接通过`android:textColor`属性来设置文本颜色。例如,如果想要设置为红色,可以这样写: ```xml &lt;TextView android:id="@+id/myTextView" android:layout_width="wrap_content" ...

    Android使用selector修改TextView中字体颜色和背景色的方法

    在字体颜色的Selector中,`android:color`用于设置字体颜色。 接下来,在布局文件中引用这些Selector,例如在一个LinearLayout中添加一个TextView: ```xml android:layout_width="match_parent" android:...

    (原创)代码动态改变某些textview文本颜色及其大小的方式

    TextView有`android:textColor`属性用于设置文本颜色,`android:textSize`属性用于设定文本大小。然而,这些属性通常在XML布局文件中静态定义。在运行时,我们可以使用以下方法动态地修改它们: 1. **改变文本颜色*...

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

    总之,Android中设置TextView字体颜色可以通过XML布局文件和Java代码两种方式实现,各有优缺点,可以根据项目需求灵活选择。无论哪种方式,都需要确保正确引用颜色资源并将其应用到TextView的 `setTextColor()` 方法...

    Android双色配置TextView功能类demo

    在Android开发中,有时我们需要为TextView实现双色显示的功能,比如在一段文本中高亮某些关键字或者区分不同的信息。这个“Android双色配置TextView功能类demo”就是针对这种需求的一个示例项目。在这个demo中,它...

    android自定义Textview动态更改颜色

    在Android开发中,自定义控件是提升应用用户体验和界面个性化的重要手段。本文将深入探讨如何自定义一个TextView,使其能够动态地改变字符颜色。这个功能对于创建吸引人的UI或者实现复杂的文字展示效果非常有用。 ...

    设计模式 策略模式 以Android 中TextView绘制文本、颜色为背景说明

    在Android开发中,`TextView`是展示文本的基本组件,它的功能非常强大,包括显示文本、设置字体样式、颜色、大小等。当涉及到文本的颜色和背景时,策略模式可以很好地发挥作用。我们可以定义不同的策略(即策略接口...

    android中自动调整宽度的TextView

    在Android开发中,TextView是用于显示文本的基本组件,它在用户界面中扮演着重要的角色。在某些场景下,我们可能需要一个可以根据文本内容自动调整宽度的TextView,以便更有效地利用屏幕空间并保持良好的布局视觉...

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

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

    android 修改listview的不同item字体的颜色

    在这里,你需要为TextView设置一个id,以便在Adapter中找到并修改它的颜色。 6. **使用Adapter**:最后,在Activity或Fragment中实例化并设置Adapter到ListView。 ```java ListView listView = findViewById(R.id....

    TextView设置

    在这个例子中,`android:textColor`属性用于设置字体颜色,我们可以使用16进制颜色码(如`#FF0000`代表红色)或者预定义的颜色资源。`android:textSize`属性则用于设置字体大小,单位通常为sp(scale-independent ...

    Android文字随下载进度渐变

    在onDraw方法中,根据当前进度,我们可以调用Shader的getColor方法获取对应的颜色值,然后使用ColorStateList或者直接设置TextView的setTextColor来改变文字颜色。 在实现过程中,我们还需要处理一些细节,比如...

    TextView设置字体格式以及滚动显示效果

    3. 字体颜色:通过`setTextColor()`方法设置字体颜色,可以使用颜色资源ID或颜色值: ```java textView.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary)); // 或 textView.setTextColor(Color....

Global site tag (gtag.js) - Google Analytics