Android programming sets the TextView color setTextColor usage instance
- 2020-12-21 18:09:50
- OfStack
This article illustrates the Android programming setting TextView color setTextColor usage. To share for your reference, the details are as follows:
The android method for setting the color of TextView, setTextColor, is overridden and can pass in two arguments.
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 each method to set the color of TextView:
TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
// plan 1 : Pass in the code argb The value of the way
tv.setTextColor(Color.rgb(255, 255, 255));
This means passing in the int color value, which is not the int value automatically assigned in the R file, so be careful. This is the color int value constructed by the static methods in the Color class.
Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
if (csl != null) {
tv.setTextColor(csl);
}
This method uses ColorStateList to get the colors configured in xml. Many that need to be configured in xml will need a mapping xml file like this.
Here's another way:
XmlResourceParser xrp = getResources().getXml(R.color.my_color);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
tv.setTextColor(csl);
} catch (Exception e) {
}
All codes:
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 when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
// plan 1 Through: ARGB The value of the way
/**
* 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
*/
// tv.setTextColor(Color.rgb(255, 255, 255));
/**
* set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',
* 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',
* 'lightgray', 'darkgray'
*/
tv.setTextColor(Color.parseColor("#FFFFFF"));
/** The original do not know the above method, with this clumsy method */
// String StrColor = null;
// StrColor = "FFFFFFFF";
// int length = StrColor.length();
// if (length == 6) {
// tv.setTextColor(Color.rgb(
// Integer.valueOf(StrColor.substring(0, 2), 16),
// Integer.valueOf(StrColor.substring(2, 4), 16),
// 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), 16),
// Integer.valueOf(StrColor.substring(4, 6), 16),
// Integer.valueOf(StrColor.substring(6, 8), 16)));
// }
// plan 2 : By resource reference
// tv.setTextColor(getResources().getColor(R.color.my_color));
// plan 3 : Write through the resource file at String.xml In the
// Resources resource = (Resources) getBaseContext().getResources();
// ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
// if (csl != null) {
// tv.setTextColor(csl);
// }
// plan 4 Through: xml Files, such as /res/text_color.xml
// XmlPullParser xrp = getResources().getXml(R.color.text_color);
// try {
// ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
// tv.setTextColor(csl);
// } catch (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);
}
}
String. xml is as follows:
<?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="http://schemas.android.com/apk/res/android">
<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 has been helpful in Android programming.