This article describes the usage of Android programming setting TextView color setTextColor. Share it for your reference, as follows:
There is a method setTextColor to set the color of the TextView in Android. This method is overloaded and two parameters can be passed in.
public void setTextColor(int color) { mTextColor = (color); updateTextColors(); } public void setTextColor(ColorStateList colors) { if (colors == null) { throw new NullPointerException(); } mTextColor = colors; updateTextColors(); }
Here are the following to write down how to use these two methods to set the color of the TextView:
TextView tv = new TextView(this); ("Test set TextView's color."); //Scheme 1: The method of passing argb value in the code((255, 255, 255));
This method is to pass in the int color value. This int is not the int value automatically allocated in the R file, so be careful. This is the color int value constructed by the static method in the Color class.
Resources resource = (Resources) getBaseContext().getResources(); ColorStateList csl = (ColorStateList) (.my_color); if (csl != null) { (csl); }
This method uses ColorStateList to get the color of the configuration in xml. Many mapping xml files that need to be configured in xml need similar mappings.
There is another way:
XmlResourceParser xrp = getResources().getXml(.my_color); try { ColorStateList csl = (getResources(), xrp); (csl); } catch (Exception e) { }
All codes:
package ; import ; import ; import ; import ; public class ListViewDemoActivity extends Activity { // private ListView listView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); TextView tv = new TextView(this); ("Test set TextView's color."); //Method 1: Through RGB value method /** * set the TextView color as the 0~255's ARGB,These component values * should be [0..255], but there is no range check performed, so if they * are out of range, the returned color is undefined */ // ((255, 255, 255)); /** * set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue', * 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', * 'lightgray', 'darkgray' */ (("#FFFFFF")); /** I didn't know if there was the above method, so I used this stupid method */ // String StrColor = null; // StrColor = "FFFFFFFF"; // int length = (); // if (length == 6) { // (( // ((0, 2), 16), // ((2, 4), 16), // ((4, 6), 16))); // } else if (length == 8) { // (( // ((0, 2), 16), // ((2, 4), 16), // ((4, 6), 16), // ((6, 8), 16))); // } //Scheme 2: Reference through resource// (getResources().getColor(.my_color)); //Scheme 3: Write it in through resource files// Resources resource = (Resources) getBaseContext().getResources(); // ColorStateList csl = (ColorStateList) (.my_color); // if (csl != null) { // (csl); // } //Scheme 4: Through the xml file, such as /res/text_color.xml// XmlPullParser xrp = getResources().getXml(.text_color); // try { // ColorStateList csl = (getResources(), xrp); // (csl); // } catch (Exception e) { // } // listView = new ListView(this); // // Cursor cursor = getContentResolver().query( // ("content://contacts/people"), null, null, null, null); // // startManagingCursor(cursor); // // ListAdapter listAdapter = new SimpleCursorAdapter(this, // .simple_expandable_list_item_2, cursor, // new String[] { "name", "name" }, new int[] { // .text1, .text2 }); // // (listAdapter); // setContentView(listView); setContentView(tv); } }
The 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:andro> <item android:state_pressed="true" android:color="#FF111111"/> <!-- pressed --> <item android:state_focused="true" android:color="#FF222222"/> <!-- focused --> <item android:state_selected="true" android:color="#FF333333"/> <!-- selected --> <item android:state_active="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 --> <item android:state_window_focused="true" android:color="#FF888888"/> <!-- window_focused --> </selector>
I hope this article will be helpful to everyone's Android programming design.