更改TextView的Textcolor和背景

时间:2012-04-05 14:59:59

标签: android colors fonts background selector

我在Android开发中完全是新手。

如果选择或按下它,我会尝试更改FontColor和ListViewItem的背景。 使用以下选择器更改背景绝对没有问题:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_focused="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_selected="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:drawable="@android:color/transparent"></item>

</selector>

但是如何更改背景和FontColor?

2 个答案:

答案 0 :(得分:2)

更改TextView的字体颜色的方式与listitem的backgroundcolor相同:创建StateListDrawable并将TextView的{​​{1}}设置为自定义drawable。

有关示例,请参阅Change ListViews text color on click。将此与您已经拥有的列表项目背景相结合。

如果您希望颜色相互淡化,请在android:textColor - 标记中设置(例如)android:exitFadeDuration="@android:integer/config_mediumAnimTime"

答案 1 :(得分:0)

试试这个

    @Override
public void onCreate(Bundle savedInstanceState) {
    TextView txtName;
    txtName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            textView.setBackgroundColor(Color.RED);
            textView.setTextColor(Color.YELLOW);
        }
    });
}