当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程设置TextView颜色setTextColor用法实例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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>

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网