Use selector to modify the color of fonts in TextView

Source: Internet
Author: User

Selector must have been used, but in changing the color of the font should be careful.

We set the font color in TextView general use

Android:textcolor= "@color/red"

But when we use selector to dynamically modify the font color, we use

[HTML]View Plaincopy
    1. android:color="@color/red"

I encountered this problem in the tabactivity, each tab is selected when the change to blue.

The code for Tab_item.xml is as follows:

[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. <ImageView
  9. android:id="@+id/iv_software_tabwidget_icon"
  10. android:layout_width="30dip"
  11. android:layout_height="30dip"
  12. android:layout_marginbottom="1dip"
  13. android:layout_margintop="5dip"
  14. android:scaletype="Fitxy" />
  15. <TextView
  16. android:id="@+id/tv_software_tabwidget_text"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginbottom="5dip"
  20. android:textcolor="@drawable/software_textcolor"
  21. android:textsize="14dip" />
  22. </linearlayout>


Note android:textcolor= "@drawable/software_textcolor", that is, Software_textcolor.xml is selector, the source code is as follows:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_selected="true" android:color="@color/software_textcolor_selected "></Item>
  4. <item android:state_selected="false" android:color="@color/software_textcolor_ Unselected "></Item>
  5. </selector>


This file is the place to note, you must use android:color= "@color/software_textcolor_selected", you cannot use the Android:textcolor property.

Additional Color.xml source code is as follows:

[HTML]View Plaincopy
    1. <? XML version= "1.0" encoding="Utf-8"?>
    2. <resources>
    3. <color name="software_textcolor_selected"> #FF1C94EA</color>
    4. <color name="software_textcolor_unselected"> #FFDCE0DF</color>
    5. </Resources>

Use selector to modify the color of fonts in TextView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.