android - 为什么涟漪效应会移除我原来的背景?

标签 android material-design

所以我尝试用自定义颜色创建波纹效果,并且有点成功,只是波纹效果移除了原始背景,从而创建了一个半透明的波纹效果,这不是我想要的。

布局:

    <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) 背景?我相信这就是 Intent ,对吧?

编辑

我现在按照 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>

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

最佳答案

为了使用您的自定义可绘制对象,您需要像这样在波纹可绘制对象中指定它 (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

关于android - 为什么涟漪效应会移除我原来的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908470/

相关文章:

android - 半透明导航栏是纯色

java - 安卓图片互相影响

Android SIP API - 如何接听来电

自定义工具栏中按钮的快速定位(谷歌设计 Material )

android - 我试图让 AutoCompleteTextView 看起来像一个 Material Edittext

angular - 重置 react 形式会触发 Angular 6 中所有必需的验证器

android - 如何使用 RecyclerView 适配器的数据并按参数将它们分成不同的组?

android - 如何在 Google Drive Android API 中获取已创建文件夹的 DriveId

android ProgressDialog 字体大小改变

material-design - 用于 PreferenceFragments 中的 DialogPreferences 的 MaterialAlertDialogBu​​ilder