How can I modify ripple color when using ?attr/selectableItemBackground as background?

前端 未结 8 1282
情深已故
情深已故 2020-11-28 18:22

I\'ve seen some SO questions and they gave some possible methods to achieve what I want. For example:

  1. Use colorControlHighlight attribute in st

相关标签:
8条回答
  • 2020-11-28 19:09

    It's showing ripple effect with color on API +21, and simple gray background on press for API -21. Add this style:

    <style name="AppTheme.MyRipple">
       <item name="colorControlHighlight">@color/your_color</item>
       <item name="android:background">?selectableItemBackgroundBorderless</item>
    </style>
    

    And set it to the view:

    <Button
       ...
       android:theme="@style/AppTheme.MyRipple" />
    
    0 讨论(0)
  • 2020-11-28 19:10

    Use the below steps:

    1. Make changes to button view in your layout.xml
    2. Add new styles in styles.xml
    

    your_layout.xml

    <Button
                            android:id="@+id/setup_submit_button"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="16dp"
                            android:text="@string/action_sign_in"
                            android:textStyle="bold"
                            android:background="@color/colorPrimary"
                            android:textColor="@color/white"
                            style="@style/SelectableItemBackground"
                            android:foreground="?android:attr/selectableItemBackground"/>
    

    -The style attribute calls the style that we created.

    -Foreground attribute calls the andorid's default selectable attribute.

    styles.xml

     <style name="SelectableItemTheme">
            <item name="colorControlHighlight">@color/white</item>
        </style>
    
    
        <style name="SelectableItemBackground">
            <item name="android:theme">@style/SelectableItemTheme</item>
            <item name="android:background">?attr/selectableItemBackground</item>
        </style>
    
    0 讨论(0)
提交回复
热议问题