Android TextView Font Color setting method summary _android

Source: Internet
Author: User
Tags color representation getcolor

The example of this article summarizes the Android TextView font Color setting method. Share to everyone for your reference, specific as follows:

for Settextview (int a) Here's a is the value of the color passed in. For example, the red 0xff0000 refers to 0xff0000 how to directly into the r.color.red is not a way to set the color, only through the article in the third way to get the color value of the resource and then pass in.

Tv.settextcolor (This.getresources (). GetColor (r.color.red));

Keywords: android textview color

TextView Font Setting Method:

1, directly through the configuration file settings
2, in the activity class to set up

The first method is simple, for setting the static or initial text color, as follows:

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  android:background= "@drawable/white"
  >
<textview android:id=
  "@+id/ Tv01 "
  android:layout_width=" fill_parent "
  android:layout_height=" wrap_content "
  android:text=" @ String/hello "
  android:autolink=" all "
  android:textcolor=" @color/red "
  />
</ Linearlayout>

Color.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
  <drawable name= "White" > #FFFFFF </ drawable>
  <drawable name= "Dark" > #000000 </drawable>
  <drawable name= "Red" > #FF0000 < /drawable>
</resources>
strings.xml
<?xml version= "1.0" encoding= "Utf-8"?>
< resources>
 <string name= "Hello" > Address:http://yahaitt.javaeye.com</string>
  <string name= " App_name "> Ya-Pear notebook </string>
</resources>

The Resources section is divided into 3 parts to make it clear, but you can also just build an XML file in the Res directory, and the file name can be arbitrarily named.

Note two places:

1, main.xml TextView label: android:textcolor= "@color/red"

2, Color.xml: <color name= "Red" > #FF0000 </color>

@color refers to the <color> tag that gets the resource file (the XML file in all res directories)

/red refers to the content under the label whose name value is red, whose value is #ff0000

So here's what we can do: android:textcolor= "@drawable/red"

@drawable refers to obtaining <drawable> tags in resource files

/red refers to the content under the label to find its name value red

And so on, I believe you will know what to do if it is in the strings.xml.

Let's look at the second way: set in the Activity class

1, the first main.xml into the following, that is, remove android:textcolor= "@color/red":

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  android:background= "@drawable/white"
  >
<textview android:id=
  "@+id/ Tv01 "
  android:layout_width=" fill_parent "
  android:layout_height=" wrap_content "
  android:text=" @ String/hello "
  android:autolink=" all "
  />
</LinearLayout>

2, modify the activity of the OnCreate method, where my activity is study03_01, the original code is as follows:

Package yahaitt.study03_01;
Import android.app.Activity;
Import Android.os.Bundle;
public class Study03_01 extends activity {    @Override public
  void OnCreate (Bundle savedinstancestate) {
    Super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
  }


The first step: Get the Text control TextView, named TV

Step two: Use the TextView SetTextColor method for text color settings, there are 3 ways to set:

The 1th kind: Tv.settextcolor (Android.graphics.Color.RED);//system with color class

2nd: Tv.settextcolor (0XFFFF00FF);//0xffff00ff is an int type of data, grouped 0x|ff|ff00ff,0x is the mark representing the color integer, FF is the transparency, the FF00FF represents the color, Note: Here the FFFF00FF must be 8 colors to indicate that FF00FF is not accepted as the color representation of the 6.

3rd: Tv.settextcolor (This.getresources (). GetColor (r.color.red))//set by obtaining a resource file. Depending on the situation r.color.red can also be r.string.red or r.drawable.red, of course, if necessary in the corresponding configuration file to do the appropriate configuration, such as:

<color name= "Red" > #FF0000 </color>
<drawable name= "Red" > #FF0000 </drawable>
< String name= "Red" > #FF0000 </string>

The detailed code is as follows:

Package yahaitt.study03_01;
Import android.app.Activity;
Import android.content.res.Resources;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Study03_01 extends activity {
  private TextView TV;
  @Override public
  void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);
    TV = (TextView) This.findviewbyid (R.ID.TV01);
    Tv.settextcolor (color.red);
    Tv.settextcolor (0xff000000);
  }


More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Communication Summary", "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with your Android programming.

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.