【问题标题】:Why does the ripple effect remove my original background?为什么涟漪效果会移除我原来的背景?
【发布时间】:2015-01-10 13:46:45
【问题描述】:

所以我正在尝试使用自定义颜色创建涟漪效果,并且有点成功,除了涟漪效果会移除原始背景并因此创建半透明的涟漪效果,这不是我想要的。

布局:

    <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>

颜色.xml

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

在叠加涟漪效果的同时,如何保持绿色 (#070) 背景?我相信这就是意图,对吧?

编辑

按照 AcademicDuck 的建议,我现在介绍了 shape

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>

现在的变化是,当我按下时,背景是纯红色而不是透明。不过还是没有涟漪。

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    如果涟漪标签上没有设置子元素,Android 涟漪效果会覆盖当前背景的顶部。在波纹标记内,指定&lt;item android:drawable = "@drawble/yourBackground"&gt;。并使“yourBackground”成为与按钮背景颜色相同的矩形可绘制资源 - #070。

    【讨论】:

    • 我无法让它工作 - 请参阅更新后的问题(在“编辑”下方)。我做错形状了吗?
    • 问题出在可绘制的波纹中。您需要在波纹标签中指定波纹的颜色。然后,您指定要在项目标签中绘制的可绘制对象。使用你原来的波纹: schemas.android.com/apk/res/android" android:color="#7f7"> 并添加一个 标签在中间,所以它看起来像这样: schemas.android.com/apk/res/android" android:color="#7f7">
    【解决方案2】:

    为了使用您的自定义可绘制对象,您需要像这样在波纹可绘制对象中指定它(RippleDrawable):

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/yourRippleColor">
        <item android:drawable="@drawable/yourDrawable" />
    </ripple>
    

    此外,还值得检查以下问题:Android L's Ripple Effect - Touch Feedback for Buttons - Using XMLandroid ripple button with background

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-14
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2014-04-08
      相关资源
      最近更新 更多