Apply Material Design Touch Ripple to ImageButton?

后端 未结 4 943
无人共我
无人共我 2020-12-23 02:41

I have an imagebutton that doesn\'t respond with a touch animation when it is clicked because it is a static image unlike regular buttons on lollipop which come with the bui

相关标签:
4条回答
  • 2020-12-23 03:17

    If you already have a background and don't want to change it, use foreground;

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@drawable/preview_in_4k_background"
        android:src="@drawable/ic_full_screen_24px"
        android:layout_marginLeft="5dp"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:layout_column="2"/>
    
    0 讨论(0)
  • 2020-12-23 03:20

    I got good answers from i.shadrin (here) and Nicolars (here).

    The difference between their answers is that ?attr/selectableItemBackgroundBorderless can give you an android.view.InflateException, so the ?android:attr/selectableItemBackground is the solution.

    FWIW, I do not know why the exception happens, because the first answer worked fine in all my old projects, but in my recent project not (maybe because the app theme = android:Theme.Material?).

    The strange thing that was happening is that though the ripple effect was shown it was out-bounding the ImageButton, so the solution is:

    • To use the android:foreground="?android:attr/selectableItemBackgroundBorderless" instead of android:background="?android:attr/selectableItemBackgroundBorderless"

    Hope it help you if you are facing the same.

    0 讨论(0)
  • 2020-12-23 03:25

    You can just add a background to your ImageButton like this :

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_dialog"
        android:background="?android:attr/selectableItemBackground" />
    
    0 讨论(0)
  • 2020-12-23 03:31

    For even better result:

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_button"
        android:background="?attr/selectableItemBackgroundBorderless"
    />
    
    0 讨论(0)
提交回复
热议问题