栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.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;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("Test set TextView's color.");
  //方案一:通过ARGB值的方式
  
//  tv.setTextColor(Color.rgb(255, 255, 255));
  
  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文件为:



 Hello World, ListViewDemoActivity!
 ListViewDemo
 #FFFFFF


/res/color/text_color.xml



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号