Activity中动态设置TextView的颜色
2016-04-25 17:25
309 查看
android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数
Java代码:[java] view
plain copy
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的颜色:
Java代码:
[java] view
plain copy
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值。
Java代码:
[java] view
plain copy
Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
if (csl != null) {
tv.setTextColor(csl);
}
这种方法是通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件。
还有种方法Java代码:
[java] view
plain copy
XmlResourceParser xrp = getResources().getXml(R.color.my_color);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
tv.setTextColor(csl);
} catch (Exception e) {
}
全部代码Java代码 :
[java] view
plain copy
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文件为:
[html] view
plain copy
<?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 代码 :
[html] view
plain copy
<?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>
相关文章推荐
- node.js cluster多进程、负载均衡和平滑重启
- Java Webservice 取数据库图片,图片以数组方式传递到Delphi前台显示
- Java 堆、栈、常量池详解
- Spring声明式事务
- 关于HashMap的源码分析
- java.lang
- Java 理论与实践: 正确使用 Volatile 变量
- 用php正则获得a标签内的文字
- nginx+memcahed+tomcat实现session的资源共享
- 机器学习中的特征选择
- Raspi下GitoLite 环境搭建
- 数论 - Funny scales(SPOJ - SCALE)
- Android 手机充值 和 手机流量充值 API
- JavaScript中的匿名函数及函数的闭包
- myeclse 加入SVN插件启动报错问题解决
- Windows下安装 MySQL 5.7.7
- python_笔记7_字符串截取(切片),有序集合,无序集合,迭代
- java 反射获取成员
- 原创docker dcos 的安装
- [改善Java代码]覆写equals方法必须覆写hashCode方法