在res/color目录下创建xml文件。
示例文件名:selector_white_gray.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="@color/Gray"/> <!-- pressed -->
<item android:color="@color/White"/> <!-- default -->
</selector>
你可以添加更多的状态。你可以使用颜色代码,如“#ffffff”而不是预定义的“@color/White”。请注意,要使用android:color而不是android:drawable。此示例在按下文本时更改文本颜色。将textColor属性设置为上面的选择器。
<TextView
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="@color/selector_white_gray"
android:textSize="18sp"
android:textStyle="bold" >
</TextView>
xml(在drawable文件夹中),你可以为每个事件状态指定颜色,以此来设置图片,并将这个xml设置为背景。
如果您的xml文件路径是'res/drawable/abc.xml',那么请将其设置为背景。android:background="@drawable/abc"
编辑以在状态xml中添加颜色
我们的xml,res/drawable/abc.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/gray" />
</selector>
然后在你的res\values\strings.xml中声明gray。
<color name="gray">#808080</color>
android:textColor="@color/mycolor"。
要更改背景,请参见其他答案。
这很容易。只需拦截所需事件并编写类似以下的内容:
TextView textView=(TextView)findViewById(R.id.myText);
String s=getString(R.string.myText);
SpannableString ss=new SpannableString(s);
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);
textView = (TextView)findViewById(R.id.myTextView);
mMainView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textView.setTextColor(Color.GREEN);//set the color here
}
});
@drawable/selector_white_grey而不是@color/selector_white_grey。 - lschlessinger