A brief summary of how to implement TextView font color settings by Android programming _android

Source: Internet
Author: User
Tags color representation getcolor

The example in this article describes how Android programming implements TextView font color settings. 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.

Copy Code code as follows:
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://www.jb51.net/</string> 
  <string name= "App_ Name > Cloud Community </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:

Copy Code code as follows:
Android:textcolor= "@color/red"

2, in Color.xml:
Copy Code code as follows:
<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:

Copy Code code as follows:
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:

Copy Code code as follows:
Tv.settextcolor (Android.graphics.Color.RED);//system self-band color class

The 2nd kind:
Copy Code code as follows:
Tv.settextcolor (0XFFFF00FF);//0xffff00ff is an int type of data
Group A 0x|ff|ff00ff,0x is a token that represents a color integer, FF is a transparency, ff00ff represents a color, note: Here FFFF00FF must be 8 color representations, do not accept ff00ff this 6 color representation.

The 3rd kind:

Copy Code code as follows:
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);
  } 


I hope this article will help you with the Android program.

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.