How to create ripple effect in simple layout

前端 未结 10 1615
耶瑟儿~
耶瑟儿~ 2020-12-02 06:06

How can I have a ripple effect in a simple linear/relative layout when touching the layout?

I\'ve tried setting the background of a layout to something like:

相关标签:
10条回答
  • 2020-12-02 06:24

    Put this into your layout:

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

    Note: There is a bug in API documentation. It will work only on API >= 23

    For all API levels use this Solution

    0 讨论(0)
  • 2020-12-02 06:26

    Simply add default ripple effect with android:background="?selectableItemBackground"

    Like:

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="?selectableItemBackground"
                android:clickable="true"
                >
    
               ...
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 06:29

    I tries all those answers and nothing worked to me, so, I solved creating the following style:

    <style name="Button.Borderless" parent="Base.Widget.AppCompat.Button.Borderless">
        <item name="android:minHeight">0dp</item>
        <item name="android:minWidth">0dp</item>
        <item name="android:layout_marginLeft">-6dp</item>
        <item name="android:layout_marginRight">-6dp</item>
        <item name="android:paddingTop">0dp</item>
        <item name="android:paddingBottom">0dp</item>
        <item name="android:paddingLeft">6dp</item>
        <item name="android:paddingRight">6dp</item>
    </style>
    

    And then, I applied to my linear layout:

    <LinearLayout
          android:id="@+id/ll_my_ripple"
          style="@style/Button.Borderless">
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 06:31
    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="#cfd8dc"
        tools:targetApi="lollipop">
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="#d1c4e9" />
            </shape>
        </item>
    </ripple>
    

    Use this ripple.xml in Drawable folder, then assign it as View's

    android:background="@drawable/ripple"

    android:clickable="true"

    0 讨论(0)
提交回复
热议问题