为什么涟漪效果会移除我的原始背景?

8

我正在尝试使用自定义颜色创建涟漪效果,有些成功,但是涟漪效果会移除原始背景,从而创建出半透明的涟漪效果,这不是我想要的。

布局:

    <Button
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:text="Clicky"
        android:colorControlHighlight="@android:color/holo_blue_light"
        android:background="@drawable/selector">
    </Button>

drawable/selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ripple"/>
    <item android:drawable="@color/normal"/>
</selector>

drawable/ripple.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#7f7">
</ripple>

color.xml

<resources>
    <color name="normal">#070</color>
</resources>

我需要怎么做才能在覆盖涟漪效果的同时保留绿色(#070)背景?我相信这是意图吧?

编辑

如AcademicDuck所建议,我现在引入了一个形状

drawable/red_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">
    <solid android:color="@color/normal" />
</shape>

这个形状被现在修改的涟漪所引用:

drawable/ripple.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawaleb="@drawable/red_shape">
</ripple>

现在的变化是,当我按下背景时,它会变成纯红色而不是透明的。不过仍然没有涟漪效果。
2个回答

1

0

如果在涟漪标签上没有设置子项,则Android涟漪效果会绘制在当前背景的顶部。在涟漪标签内,指定一个<item android:drawable="@drawble/yourBackground">。并将“yourBackground”设置为具有与按钮相同背景颜色(#070)的矩形可绘制资源。


我无法让它工作 - 请参见更新的问题(在“编辑”下面)。我是不是做错了形状? - Nilzor
问题出在涟漪可绘制对象上。您需要在涟漪标签中指定涟漪的颜色。然后,在项目标签中指定要绘制的可绘制对象。使用原始涟漪: <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#7f7"> </ripple>并在中间添加一个<item>标签,使其看起来像这样:<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#7f7"> <item android:drawable = "@drawable/red_shape"> </ripple> - AcademicDuck

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