使用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>  



  • 2020-05-21 10:56:53

    fixed z-index失效

    作为一个假的前端,在调试一个页面时出现了如下bug。 左侧的菜单固定为fixed时,二级菜单无法设置有效的z-index,导致菜单隐藏在页面元素之下,明明页面元素的z-index是1,但是无论把菜单的z-index设置为多大,都不管用。

  • 2020-05-24 08:10:18

    echarts标题(title)配置

    show:true,//显示策略,默认值true,可选为:true(显示) | false(隐藏) text: '主标题',//主标题文本,'\n'指定换行 link:'',//主标题文本超链接,默认值true target: null,//指定窗口打开主标题超链接,支持'self' | 'blank',不指定等同为'blank'(新窗口) subtext: '副标题',//副标题文本,'\n'指定换行 sublink: '',//副标题文本超链接