如何在已设置背景的TextView上应用涟漪效果

3

我希望在任何项目被点击时应用涟漪效果。但我不能为每个项目都应用它。

android:background="@color/tabColor"

背景已经设置,那么如何使用以下代码?
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"/>

我希望两者都能够正常工作,我的背景“tabColor”和水波纹效果。

尝试使用android:background="?attr/selectableItemBackgroundBorderless"。https://dev59.com/CFwX5IYBdhLWcg3wtBbG#34128965 - AskNilesh
添加水波纹效果,我之前使用过这个方法:设置水波纹效果 - Ram Mohan dubey
但是我已经添加了背景,现在如何添加涟漪效果。 - Ashpak Kureshi
答案中提供的解决方案足以创建涟漪效果。但只有在您调用setOnClickListener或使view可点击时才会出现。 - ankuranurag2
6个回答

11

你可以使用自定义的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"

6

对于水波纹效果,您应该使用android:foreground="?attr/selectableItemBackground"


1
你的 TextView 必须具备可点击属性才能使用 Ripple 效果。你可以在 xml 文件中添加 android:clickable="true" 属性或者在代码中设置点击监听器。 - Vishal Arora
请在您的代码中添加一些解释,以便其他人可以从中学习 - 为什么应该“使用”给定的内容? - Nico Haase

2

如果您已经设置了背景,请在TextView的XML代码中执行此操作。

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"

1

enter image description here

使用这个库可以获得最佳的涟漪效果.. 链接


0
将此行添加到您的style.xml中。
<item name="android:colorControlHighlight" tools:targetApi="lollipop">@color/backgroundWhite</item>

并使用视图属性:

android:foreground="?attr/selectableItemBackground"

-1

我来晚了,但以上的解决方案对我不起作用。所以我像这样使用MaterialButton

    <com.google.android.material.button.MaterialButton
        style="?attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingHorizontal="@dimen/padding_8dp"
        android:text="Clickable Text"
        android:textColor="your text color"
        app:backgroundTint="@android:color/transparent"
        app:rippleColor="ripple effect color" />

在这里,style="?attr/buttonBarButtonStyle"app:backgroundTint="@android:color/transparent"将使此按钮成为透明背景,以便它看起来像TextView,其他所有内容都将自动完成。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接