Android编程设置TextView颜色setTextColor用法实例
本文实例讲述了Android编程设置TextView颜色setTextColor用法。分享给大家供大家参考,具体如下:
android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数。
publicvoidsetTextColor(intcolor){
mTextColor=ColorStateList.valueOf(color);
updateTextColors();
}
publicvoidsetTextColor(ColorStateListcolors){
if(colors==null){
thrownewNullPointerException();
}
mTextColor=colors;
updateTextColors();
}
下边就分别写出怎么使用这两个方法设置TextView的颜色:
TextViewtv=newTextView(this);
tv.setText("TestsetTextView'scolor.");
//方案一:代码中通过argb值的方式
tv.setTextColor(Color.rgb(255,255,255));
这种方法也就是传入intcolor值,这个int不是R文件中自动分配的int值,所以要注意。这是Color类中的静态方法构造出来的颜色int值。
Resourcesresource=(Resources)getBaseContext().getResources();
ColorStateListcsl=(ColorStateList)resource.getColorStateList(R.color.my_color);
if(csl!=null){
tv.setTextColor(csl);
}
这种方法是通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件。
还有种方法:
XmlResourceParserxrp=getResources().getXml(R.color.my_color);
try{
ColorStateListcsl=ColorStateList.createFromXml(getResources(),xrp);
tv.setTextColor(csl);
}catch(Exceptione){
}
全部代码:
packagecom.txlong;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassListViewDemoActivityextendsActivity{
//privateListViewlistView;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
TextViewtv=newTextView(this);
tv.setText("TestsetTextView'scolor.");
//方案一:通过ARGB值的方式
/**
*settheTextViewcolorasthe0~255'sARGB,Thesecomponentvalues
*shouldbe[0..255],butthereisnorangecheckperformed,soifthey
*areoutofrange,thereturnedcolorisundefined
*/
//tv.setTextColor(Color.rgb(255,255,255));
/**
*settheTextViewcolorasthe#RRGGBB#AARRGGBB'red','blue',
*'green','black','white','gray','cyan','magenta','yellow',
*'lightgray','darkgray'
*/
tv.setTextColor(Color.parseColor("#FFFFFF"));
/**原来不知道有上边的方法,就用这个笨笨方法了*/
//StringStrColor=null;
//StrColor="FFFFFFFF";
//intlength=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)));
//}elseif(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)));
//}
//方案二:通过资源引用
//tv.setTextColor(getResources().getColor(R.color.my_color));
//方案三:通过资源文件写在String.xml中
//Resourcesresource=(Resources)getBaseContext().getResources();
//ColorStateListcsl=(ColorStateList)resource.getColorStateList(R.color.my_color);
//if(csl!=null){
//tv.setTextColor(csl);
//}
//方案四:通过xml文件,如/res/text_color.xml
//XmlPullParserxrp=getResources().getXml(R.color.text_color);
//try{
//ColorStateListcsl=ColorStateList.createFromXml(getResources(),xrp);
//tv.setTextColor(csl);
//}catch(Exceptione){
//}
//listView=newListView(this);
//
//Cursorcursor=getContentResolver().query(
//Uri.parse("content://contacts/people"),null,null,null,null);
//
//startManagingCursor(cursor);
//
//ListAdapterlistAdapter=newSimpleCursorAdapter(this,
//android.R.layout.simple_expandable_list_item_2,cursor,
//newString[]{"name","name"},newint[]{
//android.R.id.text1,android.R.id.text2});
//
//listView.setAdapter(listAdapter);
//setContentView(listView);
setContentView(tv);
}
}
String.xml文件为:
<?xmlversion="1.0"encoding="utf-8"?> <resources> <stringname="hello">HelloWorld,ListViewDemoActivity!</string> <stringname="app_name">ListViewDemo</string> <colorname="my_color">#FFFFFF</color> </resources>
/res/color/text_color.xml
<?xmlversion="1.0"encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:state_pressed="true"android:color="#FF111111"/> <!--pressed--> <itemandroid:state_focused="true"android:color="#FF222222"/> <!--focused--> <itemandroid:state_selected="true"android:color="#FF333333"/> <!--selected--> <itemandroid:state_active="true"android:color="#FF444444"/> <!--active--> <itemandroid:state_checkable="true"android:color="#FF555555"/> <!--checkable--> <itemandroid:state_checked="true"android:color="#FF666666"/> <!--checked--> <itemandroid:state_enabled="true"android:color="#FF777777"/> <!--enabled--> <itemandroid:state_window_focused="true"android:color="#FF888888"/> <!--window_focused--> </selector>
希望本文所述对大家Android程序设计有所帮助。