【问题标题】:Android imagebutton ripple effectAndroid imagebutton 波纹效果
【发布时间】:2017-05-01 03:49:04
【问题描述】:

好的,所以我知道涟漪效应仅适用于 LOLLIPOP 及更高版本。但是,仍然,在设置我的 ImageButton 时,我无法获得像“常规”按钮一样工作的漂亮涟漪效果,只显示图像(和透明背景)......

我添加了 AppCompat v7 并将第二个布局放在我的 drawable/layout-v21 文件夹中,其中包含以下按钮:

<ImageButton
    android:id="@+id/forward"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:width="15dp"
    android:scaleType="fitCenter"
    android:height="15dp"
    android:padding="25dp"
    style="@style/Widget.AppCompat.ImageButton"
    android:src="@drawable/forwardplay" />

但是背景是灰色的,波纹(也是灰色的)不能自定义。

所以,如果我只有一个漂亮的波纹可绘制对象放入我的 drawable-v21 那就太好了,我可以从 ImageButton 的背景属性中引用它。问题是我找不到原始的 android Ripple 效果(我有一部三星 S6 手机,是三星的主题吗?)

如果有人可以分享他们的工作绘图,我会很高兴。

谢谢!

【问题讨论】:

  • 试试这个:android:background="?attr/selectableItemBackgroundBorderless"
  • 没有。导致应用程序崩溃。为 SDK 23 编译(已经获得 v25 - 两种都试过了...)

标签: android android-5.0-lollipop rippledrawable


【解决方案1】:

试试这个,肯定可以的:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/YOUR_BUTTON_SOURCE"
    android:background="?attr/selectableItemBackgroundBorderless"
/>

【讨论】:

  • 如果您已经有不同的背景android:foreground="?attr/selectableItemBackgroundBorderless" 也可以。
  • android:foreground= 仅适用于 API 23+。如果您将背景用作透明,只需使用android:background="?attr/selectableItemBackgroundBorderless",它将使背景透明并产生漂亮的波纹效果。
【解决方案2】:

您可以为您的 Button 制作可绘制对象并设置背景。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/grey_15">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="?android:colorPrimary"/>
        </shape>
        <color android:color="@color/white"/>
    </item>
</ripple>

更改规范的颜色。

【讨论】:

  • 没有。试过那个看起来像一个不会长大的小点。
【解决方案3】:

…是三星的主题吗?

很有可能,但无论如何我都使用了以下方法来达到这个效果。

这取决于您想要的外观。如果您使用的图标类似于您在操作栏中看到的图标,您可能需要使用actionButtonStyle

style="?attr/actionButtonStyle"

您还可以选择其他一些按钮样式属性,这些属性可能包含也可能不包含涟漪效应。

否则,如果您使用实际图像作为按钮,则可以使用selectableItemBackgroundselectableItemBackgroundBorderless 属性作为前景:

android:foreground="?attr/selectableItemBackground"

【讨论】:

    【解决方案4】:

    尝试以下answer后:

    <?xml version="1.0" encoding="utf-8"?>
    <ripple android:color="#ffffff"
            xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/mask"
              android:drawable="@android:color/white" />
    </ripple>
    

    我找到了一个想法 :) 就像一个魅力!

    总结一下:

    1. 将您的布局放入 layout-v21 以支持较新的设备(并将旧设备的副本保留在常规的 drawable/layout 文件夹中。
    2. 创建一个drawable-v21文件夹并将上面的文本放在一个名为ripple.xml的文件中
    3. 像这样将其设置为背景:

      <ImageButton
          android:id="@+id/forward"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:width="15dp"
          android:scaleType="fitCenter"
          android:height="15dp"
          android:padding="25dp"
          android:background="@drawable/ripple_bg"
          android:src="@drawable/forwardplay" />
      

    【讨论】:

      【解决方案5】:

      尝试使用

      android:background="?selectableItemBackground"
      

      【讨论】:

      • 不,灰色背景看起来有点糟糕。
      【解决方案6】:

      尝试使用

      android:background="?android:attr/actionBarItemBackground"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        • 1970-01-01
        • 2015-03-06
        相关资源
        最近更新 更多