Android Programming Settings TextView Color SetTextColor usage Instances _android

Source: Internet
Author: User

This example describes the Android programming settings TextView color SetTextColor usage. Share to everyone for your reference, specific as follows:

There are methods for setting TextView colors in Android SetTextColor, this method is overloaded and can be passed in two parameters.

public void SetTextColor (int color) {
 Mtextcolor = colorstatelist.valueof (color);
 Updatetextcolors ();
}
public void SetTextColor (colorstatelist colors) {
 if (colors = = null) {
  throw new NullPointerException ();
 }
 mtextcolor = colors;
 Updatetextcolors ();
}

Here's how to use these two methods to set the TextView color:

TextView TV = new TextView (this);
Tv.settext ("Test set TextView ' s color.");
Scenario One: Code by means of a ARGB value
tv.settextcolor (Color.rgb (255, 255, 255));

This method is to pass in the int color value, which is not an int value that is automatically allocated in the R file, so be aware. This is the color int value constructed by the static method in the Color class.

Resource = (resources) Getbasecontext (). Getresources ();
Colorstatelist CSL = (colorstatelist) resource.getcolorstatelist (r.color.my_color);
if (CSL!= null) {
 tv.settextcolor (CSL);
}

This approach is achieved by colorstatelist the color of the configuration in the XML. Many of the mapping XML files that need to be configured in XML are similar.

There is a way:

Xmlresourceparser XRP = Getresources (). GETXML (R.color.my_color);
try {
 colorstatelist CSL = Colorstatelist.createfromxml (Getresources (), XRP);
 Tv.settextcolor (CSL);
} catch (Exception e) {
}

All code:

Package Com.txlong;
Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.widget.TextView;
 public class Listviewdemoactivity extends activity {//private ListView ListView; /** called the activity is a.
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  TextView TV = new TextView (this);
  Tv.settext ("Test set TextView ' s color.");  Scenario One:/** * Set the TextView color as the 0~255 ' Argb,these component values * should be [0..255], but by ARGB value There is no range check performed, so if they * are out of range, the returned color is undefined//tv.settext
  Color (Color.rgb (255, 255, 255)); /** * Set the TextView color as the #RRGGBB #AARRGGBB ' red ', ' blue ', * ' green ', ' black ', ' white ', ' gray ', ' cyan ', ' m
  AgentA ', ' Yellow ', * ' lightgray ', ' Darkgray '/Tv.settextcolor (Color.parsecolor ("#FFFFFF")); /** originally did not know to have the method above, use this stupid method///String Strcolor = null;
Strcolor = "FFFFFFFF";
int length = Strcolor.length (); if (length = = 6) {//Tv.settextcolor (COLOR.RGB (//Integer.valueof) (strcolor.substring (0, 2),//Intege
R.valueof (Strcolor.substring (2, 4),),//Integer.valueof (Strcolor.substring (4, 6), 16));     else if (length = = 8) {//Tv.settextcolor (COLOR.ARGB (///integer.valueof (strcolor.substring (0, 2), 16),// Integer.valueof (Strcolor.substring (2, 4),//Integer.valueof (Strcolor.substring (4, 6),//Integer.valu
EOf (strcolor.substring (6, 8), 16));
  //Scenario II: Resource Reference//Tv.settextcolor (Getresources (). GetColor (R.color.my_color));
Programme III: Written in String.xml through resource files/Resources resource = (Getbasecontext). Getresources ();
Colorstatelist CSL = (colorstatelist) resource.getcolorstatelist (R.color.my_color); if (CSL!= null) {//Tv.settextcolor (CSL);//}//Scenario IV: Through XML files, such as/res/text_color.xml//Xmlpullparser XRP = Getre Sources (). GETXML (r.color.tExt_color); try {//Colorstatelist CSL = Colorstatelist.createfromxml (Getresources (), XRP);//Tv.settextcolor (CSL);//} CA
  TCH (Exception e) {//}//ListView = new ListView (this);
  Cursor Cursor = Getcontentresolver (). Query (//Uri.parse ("Content://contacts/people"), NULL, NULL, NULL, NULL);
  Startmanagingcursor (cursor); ListAdapter listadapter = new Simplecursoradapter (this,//Android. r.layout.simple_expandable_list_item_2, cursor,//new string[] {"Name", "name"}, new int[] {//Android. R.id.text1, Android.
  R.ID.TEXT2});
  Listview.setadapter (ListAdapter);
  Setcontentview (ListView);
 Setcontentview (TV);

 }
}

The String.xml file is:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <string name= "Hello" >hello world, listviewdemoactivity!</string>
 <string name= "app_name" >ListViewDemo</string>
 < Color name= "My_color" > #FFFFFF </color>
</resources>

/res/color/text_color.xml

 <?xml version= "1.0" encoding= "Utf-8"?> <selector "xmlns:android=" Schemas.android.com/apk/res/android "> <item android:state_pressed=" true "android:color=" #FF111111 "/> < !--pressed--> <item android:state_focused= "true" android:color= "#FF222222"/> <!--focused--> <i TEM android:state_selected= "true" android:color= "#FF333333"/> <!--selected--> <item android:state_activ E= "true" android:color= "#FF444444"/> <!--active--> <item android:state_checkable= "true" android:color= #FF555555 "/> <!--checkable--> <item android:state_checked=" true "android:color=" #FF666666 "/> -Checked--> <item android:state_enabled= "true" android:color= "#FF777777"/> <!--enabled--> <ite

M android:state_window_focused= "true" android:color= "#FF888888"/> <!--window_focused--> </selector> 

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.