使用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-08-16 16:09:30

    android WebView 注入js 几种方式

    有时我们开发中需要将js 注入到我们本地,有可能你会说,放在Web不就可以了吗,的确,但是需求就是这样的

  • 2020-11-05 23:20:29

    mac更新node版本

    initializer function 0x0 not in mapped image for /usr/local/bin/node,除了上面的问题 你或许还出现过 no such file or directory 这样的提示,总之更新完以后node直接不能用了。

  • 2020-11-07 16:31:02

    nginx配置X-Frame-Options允许多个域名iframe嵌套

    有时候我们需要允许多个url的来源,但是又不能全部开放,所以应该匹配第三种方法ALLOW-FROM url,那么多个url该如何配置呢,百度了所有网站都没有找到,那么这里写给大家,其实很简单: add_header X-Frame-Options 'ALLOW-FROM https://xxx.xxxxxx.com https://xxx2.xxxxxxx.com'; 就是使用空格隔开就好了!

  • 2020-11-08 08:31:51

    meteor在不同端口启动服务

    当没有任何参数时,run是默认行为,在幕后,它3000端口开启node.js服务器实例,同时开启监听3001端口的MongoDB服务

  • 2020-11-11 15:05:39

    nuxt如何在其它js文件中使用store

    在新建的js文件中想用store里面的数据,比如token想在封装的axios里面,请求头里面去使用,亦或者通过app的JS接口获取token并存储在store里面。我们都知道如何在vue中如何使用。