【转】Android中设置TextView的颜色setTextColor
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html
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>
- ListViewDemo.zip (14.5 KB)
- 下载次数: 14
【转】Android中设置TextView的颜色setTextColor的更多相关文章
- [转]Android中设置TextView的颜色setTextColor
[转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...
- Android中设置TextView的颜色setTextColor
tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...
- 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...
- android中设置TextView/Button 走马灯效果
在Android的ApiDemo中,有Button的走马灯效果,但是换作是TextView,还是有一点差异. 定义走马灯(Marquee),主要在Project/res/layout/main.xml ...
- Android 中 设置TextView垂直滚动
布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...
- android中用Spannable在TextView中设置超链接、颜色、字体
昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类.首先,看一下其运行效果: 要给 TextView 加上效果,方式主要有几种: 第一种,自动 ...
- 【原创】如何在Android中为TextView动态设置drawableLeft等
如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 Drawable drawable = getResources().getD ...
- Android中对TextView中的部分内容的字体样式的设置方法
Android中的TextView中内容,有时候需要对其部分内容添加下划线和颜色操作: String str = "回复 " + uname + " 的评论: " ...
- Android中string.xml文件中设置部分字体颜色大小
1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font colo ...
随机推荐
- 【Android】Android的优点和不足之处
随着Android的越来越红火,不少应聘Android开发的人,难免会被问到这样的问题,就是这个平台的优点,当然有优点也会有缺点的, 下面是我从网上总结出来的,希望对大家应聘Android开发有所帮助 ...
- HDU 4296 Buildings(贪心)
题意: 给定n个建筑物,每个建筑物都有两个属性w, s,每个建筑物都有一个PDV = (Σw j)-s i .意思就是它上面的所有的w相加减去它的s,让求怎么放置这个建筑物使得每个建筑物当中PDV最大 ...
- ASP.NET mvc 遇见的问题
1.数据库配置 The specified named connection is either not found in the configuration, not intended to be ...
- Spring中的创建与销毁
在bean中添加属性init-method="方法名" destroy-method="方法名" init-method 该方法是由spring容 ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- Python直接迭代序列比通过索引迭代序列快。
小脚本跑一下看看时间. 原理:直接迭代序列是通过Python内置的迭代器去实现的,而如果迭代序列需要先造一个可迭代的序列出来.内置的迭代器并不是一下将所有的数据放入内存中,而是需要多少取多少. #!/ ...
- underscorejs-partition学习
2.25 partition 2.25.1 语法: _.partition(list, predicate, [context]) 2.25.2 说明: 拆分list为两个数组. 第一个数组的元素都满 ...
- MySQL5.7.9免安装版配置方法
1. 解压MySQL压缩包 将下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\Program Files\mysql-5.7.9-win32" ...
- 每个Linux新手都应该记住的10个基本Linux命令
Linux对我们的生活有着很大的影响.至少,你的安卓手机上面就有Linux内核.然而,头一次入手Linux只会让你觉得不适.因为在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你 ...
- MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...