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

2017-04-16 00:27:31

[html] view plain copy

  1.   



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

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

Android:textColor="@color/red"

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

[html] view plain copy

  1. android:color="@color/red"  



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

tab_item.xml的代码如下:

[html] view plain copy

  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 plain copy

  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 plain copy

  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>  



  • 2021-01-05 15:41:42

    nodejs修改时区

    ​let date = new Date(); date.setHours(date.getHours() + 8);

  • 2021-01-06 23:09:38

    mp3解码器转PCM合并

    首先,为了混合两个音频文件,您需要操纵它们的原始表示;由于MP3文件被压缩,您无法直接访问信号的原始表示.您需要对压缩的MP3流进行解码,以便“理解”您的音频信号的波形,然后可以混合使用.

  • 2021-01-08 16:47:37

    nodejs如何使用fetch

    node 中没有实现 fetch,你可以使用 node-fetch,使得在 node 中也可以使用 fetch.

  • 2021-01-08 16:49:59

    CommonJs 与 ESModule区别

    node中模块导入require是一个内置的函数,因此只有在运行后我们才可以得知模块导出内容,无法做静态分析