Android 在TextView对象上设置背景并保持涟漪效果

Android 在TextView对象上设置背景并保持涟漪效果,android,background,android-imageview,textview,rippledrawable,Android,Background,Android Imageview,Textview,Rippledrawable,我想以编程方式设置TextView对象的背景,但也会对其产生连锁反应 我可以在后台设置为android:selectableItemBackground的情况下使用,但在设置背景时,涟漪效应会消失 我还尝试将ImageView与TextView放在FrameLayout中。并将图像设置为TextView的背景,而不是ImageView的图像:是的,ripple在那里,但它似乎在图像的“后面” 是否需要从位图为背景创建一个RippleDrawable?我该怎么做?RippleDrawable是解决

我想以编程方式设置TextView对象的背景,但也会对其产生连锁反应

我可以在后台设置为android:selectableItemBackground的情况下使用,但在设置背景时,涟漪效应会消失

我还尝试将
ImageView
TextView
放在
FrameLayout
中。并将图像设置为
TextView
的背景,而不是
ImageView
的图像:是的,ripple在那里,但它似乎在图像的“后面”


是否需要从位图为背景创建一个
RippleDrawable
?我该怎么做?

RippleDrawable是解决方案:

RippleDrawable newImage = new RippleDrawable(
        new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_pressed},
                        new int[]{}
                },
                new int[] {
                        getResources().getColor(R.color.ripple_material_light),
                        getResources().getColor(R.color.ripple_material_dark),
                }),
                new BitmapDrawable(getResources(), mapBitmap),
                null);
textView.setBackground(newImage);

我和您有相同的问题,并使用
框架布局上的android:前台=“?attr/selectableItemBackground”

以下是框架布局示例:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/more"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground">
    <ImageView
        android:id="@+id/discover_carousel_item_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:scaleType="fitXY" />
</FrameLayout>


BitmapDrawable构造函数中的
mapBitmap
参数是什么?这是一个
位图
RippleDrawable
仅在API版本21时可用。我不知道
前台
是否在特定情况下工作,但在我的情况下,我将其切换到
后台
,当触摸视图时,它开始显示涟漪效果。