首页 > 解决方案 > how to apply ripple effect on textview when already background set

问题描述

I want to apply ripple effect when any item click. But I can't apply each and every item

android:background="@color/tabColor"

background already set so how to use following code

android:background="?android:attr/selectableItemBackground"
<TextView    
            android:id="@+id/mistake_btn_tv"    
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/tabColor"
            android:gravity="center"
            android:requiresFadingEdge="none"
            android:text=""
            android:textColor="@color/md_white_1000"
            android:textSize="@dimen/txt_20"
            android:textStyle="bold"
            android:typeface="monospace"/>

I expect both are working there, my background "tabColor" and also ripple effect

标签: androidmaterial-designrippledrawable

解决方案


您可以使用自定义drawable文件来获得涟漪效果。在这里,我与您分享代码。只需实现该代码。希望它会正常工作。

  <?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="@color/colorAccent"
    tools:ignore="NewApi">
    <item android:id="@android:id/background">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">

            <solid android:color="@color/white"/>
            <corners
                android:radius="5dp"/>
            <stroke android:width="1dp" android:color="@color/gray_text"/>
            <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />

        </shape>
    </item>
</ripple>

并将其设置为背景:-

  android:background="@drawable/border_ripple_gray"

推荐阅读