Android TextView disabling color-changing

2019-07-28 15:05发布

问题:

I have a TextView with the following XML (it's the only thing in the view):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ipg_display"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="textview"
  android:scrollbars = "vertical"
  android:fillViewport = "true" android:clickable="false"/>

The problem I'm having is that when the text view is clicked by the user (to drag through it or whatever), the text changes color slightly as if the view is being "clicked". How do I disable this?

I was hoping that settings "clickable" to false would solve the issue, but no luck :(

What am I missing?

回答1:

use the following selector for your button as the background. and make sure every item has the same color.

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_focused="true"
    android:drawable="@color/yourbackgroundcolor"/>
   <item 
    android:state_pressed="true"
    android:drawable="@color/yourbackgroundcolor"/>
  <item android:drawable="@color/yourbackgroundcolor"/>
</selector>

HTH.