android - 如何使用支持库实现波纹动画?

标签 android material-design

我正在尝试在按钮单击时添加波纹动画。我确实喜欢下面,但它需要 minSdKVersion 到 21。

ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

按钮

<com.devspark.robototextview.widget.RobotoButton
    android:id="@+id/loginButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ripple"
    android:text="@string/login_button" />

我想让它向后兼容设计库。

如何做到这一点?

最佳答案

基本波纹设置

  • View 中包含的涟漪。
    android:background="?selectableItemBackground"

  • 超出 View 边界的涟漪:
    android:background="?selectableItemBackgroundBorderless"

    看看here用于解析 Java 代码中的 ?(attr) xml 引用。

支持库

  • 使用 ?attr:(或 ? 简写)而不是 ?android:attr 引用 support library ,因此可返回到 API 7。

带有图像/背景的涟漪

  • 要拥有图像或背景并覆盖波纹,最简单的解决方案是将 View 包装在 FrameLayout 中,并使用 setForeground() 设置波纹code> 或 setBackground().

老实说,否则没有干净的方法可以做到这一点。

关于android - 如何使用支持库实现波纹动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26604134/

相关文章:

android - 我可以在运行时以编程方式设置 "android:layout_below"吗?

android - 在同一部安卓手机中安装开发应用程序和播放商店应用程序

html - Bootstrap Material 设计输入字段没有动画

java - Dagger 2 和接口(interface)实现

android - 如何将 SurfaceTexture 保存为位图

android - MaterialComponents.TextInputLayout.OutlinedBox 它不能正常工作 boxBackgroundColor

android - 单击工具栏导航图标会使应用程序崩溃

css - 如何增加 Material UI 下拉菜单的宽度

android - 如何在 Google android 应用程序后退按钮上做按钮点击动画?

android - 使用较大的数据集快速滚动 ListView 时出现 ANR