Android编程设置TextView颜⾊setTextColor⽤法实例本⽂实例讲述了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.b(255, 255, 255));
这种⽅法也就是传⼊int color值,这个int不是R⽂件中⾃动分配的int值,所以要注意。这是Color类中的静态⽅法构造出来的颜⾊int值。
Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) _color);
if (csl != null) {
tv.setTextColor(csl);
}
这种⽅法是通过ColorStateList得到xml中的配置的颜⾊的。好多需要xml中配置的都要类似这样的映射xml⽂件。
android编程入门指南 pdf还有种⽅法:
XmlResourceParser xrp = getResources()._color);
try {
ColorStateList csl = ateFromXml(getResources(), xrp);
tv.setTextColor(csl);
} catch (Exception e) {
}
全部代码:
long;
import android.app.Activity;
aphics.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) {
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.b(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.b(
//    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()._color));
/
/⽅案三:通过资源⽂件写在l中
//  Resources resource = (Resources) getBaseContext().getResources();
//  ColorStateList csl = (ColorStateList) _color); //  if (csl != null) {
//  tv.setTextColor(csl);
//  }
//⽅案四:通过xml⽂件,如/res/l
//  XmlPullParser xrp = getResources()._color);
//  try {
//  ColorStateList csl = ateFromXml(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.1, android.2 });
//
// listView.setAdapter(listAdapter);
// setContentView(listView);
setContentView(tv);
}
}
<?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/l
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android/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>
希望本⽂所述对⼤家Android程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。