【问题标题】:Ripple effect for lollipop views棒棒糖视图的涟漪效应
【发布时间】:2014-11-21 21:00:51
【问题描述】:

我一直在为 Lollipop (API 21) 开发应用程序。

当我将Button 颜色更改为某种颜色时,涟漪效果不起作用。

我找到了一些用于涟漪效应的第三方库,但我想使用标准 API 来实现。

这个answer 也没有帮助。

XML:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:background="@android:color/holo_blue_bright"
    android:layout_alignParentStart="true"
    android:layout_marginTop="76dp"
   />

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    您必须将按钮的背景设置为可以在 XML 中定义的 RippleDrawable。 (我将其命名为holo_blue_ripple.xml

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/white"> <!-- ripple color -->
    
        <item android:drawable="@android:color/holo_blue_bright"/> <!-- normal color -->
    
    </ripple>
    

    然后用android:background="@drawable/holo_blue_ripple"引用它。

    【讨论】:

    • 感谢您的帮助。但我想知道如何使用这种技术应用圆角半径?我在holo_blue_ripple.xml 中添加了以下行,但它不起作用:&lt;item&gt; &lt;shape android:shape="rectangle"&gt; &lt;corners android:radius="15dip"/&gt; &lt;/shape&gt; &lt;/item&gt;
    • 我想出了解决方案。这是:&lt;ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@android:color/holo_blue_bright"&gt; &lt;!-- ripple color --&gt; &lt;item&gt; &lt;shape android:shape="rectangle"&gt; &lt;corners android:radius="5dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:drawable="@android:color/holo_blue_dark"/&gt; &lt;!-- normal color --&gt; &lt;/ripple&gt;
    • 棒棒糖前的版本怎么办?
    • 把这个xml放在哪里? Bcz 波纹在 21 岁以下不起作用。
    • @Prince 你可以把它放在drawable-v21 文件夹中。
    【解决方案2】:

    试试这个:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        ...
     />
    

    基于this tutorialthe official docs

    【讨论】:

    • 除非你想自定义你的涟漪效果,否则我认为这是最简单的解决方案。
    • 对我不起作用:/效果是-单击时颜色变化。我的安卓版本只有5.0。你能帮忙吗?
    【解决方案3】:

    同样?attr/selectableItemBackgroundBorderless可以用来做圆形波纹效果。

    【讨论】:

    • 对我不起作用:/效果是-单击时颜色变化。我的安卓版本只有5.0。你能帮忙吗?
    【解决方案4】:

    试试这个属性

    android:background="?attr/selectableItemBackground"
    

    这是在视图/布局中显示涟漪效果的最简单方法。

    【讨论】:

    • 对我不起作用:/ 效果是 - 单击时颜色变化。我的安卓版本只有5.0。你能帮忙吗?将此添加到按钮
    猜你喜欢
    • 2015-08-25
    • 2015-09-24
    • 2016-02-04
    • 2016-02-25
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多