2012-04-05 42 views
1

我在Android开发中总是很新颖。更改Textcolor和TextView的背景

我尝试改变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

更改的TextView的字体颜色以同样的方式作为列表项的的backgroundColor做:创建一个StateListDrawable并设置TextViewandroid:textColor到您的自定义绘制。请参阅Change ListViews text color on click。将此与您已经拥有的listitem背景相结合。

如果您想让颜色淡入淡淡的色彩,请在<selector> -tag中设置(例如)android:exitFadeDuration="@android:integer/config_mediumAnimTime"

+0

谢谢,就是这样。 也谢谢你特别注意淡化颜色。 – Sandman 2012-04-10 08:17:37

+0

在这种情况下,请将答案标记为已接受;) – Reinier 2012-04-10 11:18:10

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); 
     } 
    }); 
} 
+0

Minh Binh可以请你告诉我如何在textview的情况下在悬停时更改textcolor,而我的textview在行内 – Erum 2014-06-19 15:38:00