Default selector background in Clickable Views

前端 未结 9 1237
执念已碎
执念已碎 2020-12-23 02:15

I have some clickable views and I want to set the default available background that is present on list click (in ICS is a blue color). I have tried putting as background thi

相关标签:
9条回答
  • 2020-12-23 02:31

    If you are using appcompat-v7, you can just use ?attr/selectableItemBackground.

    0 讨论(0)
  • 2020-12-23 02:35

    This works fine on api 11 and above. But as noted it wont work on previous versions.

    android:background="?android:attr/selectableItemBackground"
    

    Here is a solution to have it run on all versions running android.

    1. Add the appropriate colors within the colors.xml which is located within your values folder. It should appear as such:

      <color name="white">#ffffff</color>
      <color name="blue">#7ecce8</color>
      
    2. Create an xml selector file. Here I named it button_selection.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true" android:drawable="@color/blue"/>         <!--pressed -->
      <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
      <item android:drawable="@color/white"/> <!-- default -->
      </selector> 
      
    3. Go to your view or button and set the newly created button_selection.xml as its background.

      android:background="@drawable/button_selection"
      
    0 讨论(0)
  • 2020-12-23 02:37

    This works for me:

    android:background="?android:attr/selectableItemBackground"
    
    0 讨论(0)
提交回复
热议问题