【问题标题】:Using Elevation and Ripple in ImageButton在 ImageButton 中使用高程和波纹
【发布时间】:2016-08-30 06:17:34
【问题描述】:

我正在尝试在 ImageButton 上同时设置高程和波纹效果。 我正在运行 LOLLIPOP。

代码如下:

    <android.support.v7.widget.AppCompatImageButton
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            app:srcCompat="@drawable/add" />

我的主题是:Theme.AppCompat.Light.DarkActionBar

使用此代码,我只会得到涟漪。 如果我删除背景(波纹),我会得到提升。

已编辑:

注意:所有屏幕截图都是在点击图片时截取的(长按)。

我用android:background="?attr/selectableItemBackground" 进行了测试,我得到了一个波纹,背景是一个正方形,但没有高程:

使用android:background="?attr/selectableItemBackgroundBorderless",我得到了一个圆形背景的波纹,但没有高程:

没有背景属性我得到了海拔,默认ImageButton背景:

我需要的是同时具有圆形波纹背景和高程。

【问题讨论】:

  • 您使用的是无边框背景。无边框元素不会有阴影。这就是为什么。
  • 你用的是什么主题?
  • 我已经用该信息更新了问题。
  • 解决了吗?

标签: android android-5.0-lollipop android-elevation


【解决方案1】:

现在已经五年了,但我重新审视了这一点,一个解决方案是使用 foreground 而不是 background:

    <ImageButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:elevation="5dp"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:src="@mipmap/ic_launcher"/>

【讨论】:

    【解决方案2】:

    像阴影一样,无边界的涟漪会投射到最近的父表面上——这意味着它们不能投射阴影。考虑改用带边框的波纹:

    android:background="?attr/selectableItemBackground"
    

    【讨论】:

    • 感谢您的回答,请看一下已编辑的问题。
    猜你喜欢
    • 2017-05-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多