Font color settings in Android

Source: Internet
Author: User
Tags getcolor

1. Definition of color values in Android:

The color value is defined by RGB primary colors and an Alpha value. The color value is defined as a # At the beginning, followed by the Alpha-red-green-blue format. For example, # RGB # argb # rrggbb # aarrggbb.

2.Definition of the Color Resource XML file

I. resource location: the Res/values/colors. xml file name is not limited to any conforming name of color.

Ii. Color XML file format 1.

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> root element <color> child element: <color name = color_name> # color_value </color>
    </resources>

For example:

<?xml version="1.0" encoding="utf-8"?>    <resources>      <color name=white>#ffffff</color> </resources>

2.

<?xml version="1.0" encoding="utf-8"?>  <resources>  <drawable name="white">#ffffffff</drawable></resources>

2. Reference of color in XML: 1

android:textColor="@color/white"
 2android:textColor="@drawable/white"

And so on, I believe you know what to do in strings. xml and how to do it in style ..
3. color reference in Java code

 tv = (TextView)this.findViewById(R.id.tv01);            1 tv.setTextColor(Color.RED);            2 tv.setTextColor(0xff000000);         3  Resources rs = this.getResources(); tv.setTextColor(rs.getColor(R.drawable.red));       4  tv.setTextColor(Color.rgb(red, green,blue));   

1st types: TV. settextcolor (Android. Graphics. color. Red); // color class that comes with the System

2nd types: TV. settextcolor (0xffff00ff); // 0xffffff00ff is int type data, grouped by 0x | FF | ff00ff, 0x indicates the color integer, FF indicates the transparency, and ff00ff indicates the color, note: Here, ffff00ff must be represented by 8 colors, and 6 colors such as ff00ff are not accepted.

3rd types: TV. settextcolor (this. getresources (). getcolor (R. color. Red); // you can set it by obtaining the resource file. Depending on the situation, R. color. Red can also be R. String. Red or R. drawable. Red. Of course, the premise is that you need to configure it in the corresponding configuration file.

By setting the text color in the code, we can achieve the dynamic text color.


Related Article

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.