zoukankan      html  css  js  c++  java
  • android TextView的字体颜色设置的多种方法(续)

    继《android TextView的字体颜色设置的多种方法》【http://yahaitt.iteye.com/blog/454439

     

    下面看看第二种方式:在Activity类中进行设置

     

    1、先将main.xml改成如下,即去掉android:textColor="@color/red":

     

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     android:background="@drawable/white"  
    7.     >  
    8. <TextView    
    9.     android:id="@+id/tv01"  
    10.     android:layout_width="fill_parent"   
    11.     android:layout_height="wrap_content"   
    12.     android:text="@string/hello"  
    13.     android:autoLink="all"  
    14.     />  
    15. </LinearLayout>  

     2、修改Activity的onCreate方法,这里我的Activity是Study03_01,原始代码如下:

     

     

    Java代码  收藏代码
    1. package yahaitt.study03_01;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5.   
    6. public class Study03_01 extends Activity {  
    7.     @Override  
    8.     public void onCreate(Bundle savedInstanceState) {  
    9.         super.onCreate(savedInstanceState);  
    10.         setContentView(R.layout.main);  
    11.     }  
    12. }  

     

    第一步:获得文本控件TextView,取名为tv

     

    第二步:通过TextView的setTextColor方法进行文本颜色的设置,这里可以有3种方式进行设置:

     

    第1种:tv.setTextColor(android.graphics.Color.RED);//系统自带的颜色类

     

    第2种:tv.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。

     

    第3种:tv.setTextColor(this.getResources().getColor(R.color.red));//通过获得资源文件进行设置。根据不同的情况R.color.red也可以是R.string.red或者R.drawable.red,当然前提是需要在相应的配置文件里做相应的配置,如:

    <color name="red">#FF0000</color>

    <drawable name="red">#FF0000</drawable>

    <string name="red">#FF0000</string>

     

     

    详细的代码如下:

    Java代码  收藏代码
    1. package yahaitt.study03_01;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.res.Resources;  
    5. import android.graphics.Color;  
    6. import android.os.Bundle;  
    7. import android.widget.TextView;  
    8.   
    9. public class Study03_01 extends Activity {  
    10.     /** Called when the activity is first created. */  
    11.     private TextView tv;  
    12.     @Override  
    13.     public void onCreate(Bundle savedInstanceState) {  
    14.         super.onCreate(savedInstanceState);  
    15.         setContentView(R.layout.main);  
    16.           
    17.         tv = (TextView)this.findViewById(R.id.tv01);  
    18.           
    19. //        tv.setTextColor(Color.RED);  
    20.           
    21. //        tv.setTextColor(0xff000000);  
    22.        /*  
    23.         Resources rs = this.getResources(); 
    24.         tv.setTextColor(rs.getColor(R.drawable.red)); 
    25.         */  
    26.   
    27.     }  
    28. }  

     

    注:请切换相应的注释

     

     

    通过在Activity类中设置文本颜色,我们可以实现文本颜色的动态化。如果想保持文本颜色静态不变的话,可以直接通过上一篇中讲的通过直接配置即可。

  • 相关阅读:
    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) D
    HDU 2036 求任意多边形面积板子题
    HDU 6703 array(主席树)
    2019牛客暑期多校训练营(第九场)H Cutting BamboosO(二分、主席树)
    lintcode-425-电话号码的字母组合
    lintcode-81-数据流中位数
    lintcode-424-逆波兰表达式求值
    lintcode-423-有效的括号序列
    lintcode-422-最后一个单词的长度
    lintcode-421-简化路径
  • 原文地址:https://www.cnblogs.com/firecode/p/2836774.html
Copyright © 2011-2022 走看看