在TextView对象上设置背景并保持波纹效果 [英] Set background on TextView object and maintain ripple effect
问题描述
我想以编程方式设置TextView对象的背景,但也会对此产生连锁反应.
I want to set the background of a TextView object programmatically, but also have a ripple effect for it.
我可以将背景设置为android:selectableItemBackground
,但是在设置背景时波纹效果会消失.
I can use with background set to android:selectableItemBackground
, but the ripple effect gets lost, when setting the background.
我还尝试将ImageView
和TextView
放在FrameLayout
中.并将图像设置为不是TextView
的背景,而是设置为ImageView
的图像:是的,有波纹,但它似乎在图像的后面".
I also tried putting an ImageView
together with the TextView
in a FrameLayout
. And set the image not as the background of the TextView
, but as image of the ImageView
: Yes, ripple is there, but it appears to be "behind" the image.
是否需要从位图为背景创建RippleDrawable
?我该怎么办?
Do I need to create a RippleDrawable
from the bitmapfor the background? How would I do that?
推荐答案
我遇到了与您相同的问题,并使用
进行了修复
android:foreground="?attr/selectableItemBackground"
在FrameLayout上.
I had same problem as you and fixed it using
android:foreground="?attr/selectableItemBackground"
on FrameLayout.
这是示例FrameLayout:
Here is the sample FrameLayout:
<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>
这篇关于在TextView对象上设置背景并保持波纹效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!