首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >笔记15 | 归纳总结Android动态设置TextView的颜色的四种方法

笔记15 | 归纳总结Android动态设置TextView的颜色的四种方法

作者头像
项勇
发布2018-06-19 15:11:55
1.1K0
发布2018-06-19 15:11:55
举报
文章被收录于专栏:项勇项勇

前言

本节简要地来归纳总结Android动态设置TextView的颜色的四种方法

地址

http://blog.csdn.net/xiangyong_1521/article/details/51307590

目录

  • 主要代码
  • 链接

一. 代码示例

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);    
    }    
}

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 地址
  • 目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档