• 使用selector修改TextView中字体的颜色


    selector想必大家都用过了,但是在修改字体的颜色的时候还是要细心。

    我们在TextView中设置字体颜色一般使用 

    android:textColor="@color/red"

    但是我们在使用selector动态修改字体颜色的时候要使用

    [html] view plaincopy
     
    1. android:color="@color/red"  

    我遇到这个问题的时候是在TabActivity中,每个Tab在选中的时候修改为蓝色。

    tab_item.xml的代码如下:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:id="@+id/ll_software_tabwidget_item"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     android:gravity="center_horizontal"  
    7.     android:orientation="vertical" >  
    8.   
    9.    <ImageView  
    10.         android:id="@+id/iv_software_tabwidget_icon"  
    11.         android:layout_width="30dip"  
    12.         android:layout_height="30dip"  
    13.         android:layout_marginBottom="1dip"  
    14.         android:layout_marginTop="5dip"  
    15.         android:scaleType="fitXY" />  
    16.   
    17.     <TextView  
    18.         android:id="@+id/tv_software_tabwidget_text"  
    19.         android:layout_width="wrap_content"  
    20.         android:layout_height="wrap_content"  
    21.         android:layout_marginBottom="5dip"  
    22.         android:textColor="@drawable/software_textcolor"  
    23.         android:textSize="14dip" />  
    24.       
    25. </LinearLayout>  


    注意android:textColor="@drawable/software_textcolor",即software_textcolor.xml就是selector,源码如下:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    3.   
    4.     <item android:state_selected="true" android:color="@color/software_textColor_selected"></item>  
    5.     <item android:state_selected="false" android:color="@color/software_textColor_unselected"></item>  
    6.   
    7. </selector>  


    这个文件中就是要注意的地方了,必须使用android:color="@color/software_textColor_selected",不能使用android:textColor属性。

    另附color.xml的源码如下:

    [html] view plaincopy
     
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <resources>  
      3.   
      4.     <color name="software_textColor_selected">#FF1C94EA</color>  
      5.     <color name="software_textColor_unselected">#FFDCE0DF</color>  
      6.       
      7. </resources>  
  • 相关阅读:
    周赛D. Digging for Gold(扫描线)
    CF1209F Koala and Notebook(最短路+拆点)
    P6793 [SNOI2020]字符串(后缀树上DP)
    [HEOI2016/TJOI2016]字符串(后缀自动机,可持久化线段树,线段树合并,二分答案)
    CF1166F Vicky's Delivery Service(并查集,启发式合并)
    P4248 [AHOI2013]差异(后缀树)
    CF1175F The Number of Subpermutations(单调栈,ST表)
    CF666E Forensic Examination(后缀自动机,可持久化线段树合并)
    GYM103069G. Prof. Pang's sequence
    [转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
  • 原文地址:https://www.cnblogs.com/exmyth/p/4603392.html
Copyright © 2020-2023  润新知