0

我试图通过我在drawable中创建的自定义xml文件在我的应用中实现涟漪效应。现在,它只适用于运行棉花糖或以上的设备而不是棒棒糖(api 21 & api 22)。这是自定义的xml文件。波纹效果不适用于棒棒糖,但适用于棉花糖及以上

RES /绘制-V21/ripple_effect.xml

<?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="#ff0000" 
    tools:targetApi="LOLLIPOP"> 
<item android:id="@android:id/mask"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#ff0000" /> 
    </shape> 
</item> 

这是我的主要活动XML

布局/ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@null" 
    android:clickable="true" 
    android:foreground="@drawable/ripple_effect" 
    android:src="@drawable/background3360" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#333" 
    android:foreground="@drawable/ripple_effect" 
    android:text="This is a Button" 
    android:textColor="#fff" /> 

</LinearLayout> 

现在启动后,我可以清楚地看到一个涟漪效应(红色)在imageView和按钮上,但在棒棒糖设备上,我没有看到这种效果。我的猜测是,这是因为android:foreground,它不知道在除棒棒糖framelayout以外的布局。以上棒棒糖android:foreground适用于各种布局/视图。我想在此linearlayoutimageView以及棒棒糖设备中通过xml实现此自定义连锁反应。

+0

你是正确的,Android的:不支持在大多数的Android版本的标准视图前景。但是,您可以创建支持此属性的自定义ImageView,也可以将ImageView包装在支持该属性的FrameLayout中。 – BladeCoder

回答

0

中添加android:background="?android:attr/selectableItemBackground"LinearLayout

+0

谢谢你的回复。这只是对整个布局应用默认纹波效果。我想要自己的自定义连锁效应。 –

相关问题