zoukankan      html  css  js  c++  java
  • 使用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>  
  • 相关阅读:
    centos7 & ubuntu14.02安装sublime 3
    flask之flask-restful
    ubuntu14.04安装python3.7.1
    vim中多行注释和多行删除命令
    python3之scrapy安装使用
    python3 之 linux命令实现
    ubuntu14.04安装pyspider
    升级3.4成3.6 ubuntu14.04 和miniconda虚拟环境
    python3 之初学者常犯的5个错误
    python3 之 格式化json
  • 原文地址:https://www.cnblogs.com/exmyth/p/4603392.html
Copyright © 2011-2022 走看看