【问题标题】:How to set button background drawable image with ripple effect in android如何在android中设置具有波纹效果的按钮背景可绘制图像
【发布时间】:2016-09-28 04:28:50
【问题描述】:

我的 XML 代码:

<Button
    android:id="@+id/link_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/google_btn" />

我正在应用默认的涟漪效果

<Button
    android:id="@+id/link_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground" />

但我需要按钮背景"@drawable/google_btn"
"?android:attr/selectableItemBackground"。这意味着我需要自定义背景的涟漪效果。

【问题讨论】:

  • 我该怎么做
  • 值得强调的是(正如@zahidul 提到的),涟漪效应仅适用于 Android v21 及更高版本。

标签: android background android-drawable ripple


【解决方案1】:

在您的drawable-v21 文件夹中,您可以自己编写用于涟漪效应的代码。制作一个可绘制的 xml 文件并通过ripple 设置起始标签。像这样:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccentDark">
    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item
                android:drawable="@color/button_accent_dark"
                android:state_checked="false"/>
            <item
                android:drawable="@color/button_accent"
                android:state_checked="true" />
            <item
                android:drawable="@color/button_accent_dark" />

        </selector>
    </item>
</ripple>

【讨论】:

  • 谢谢@Zahidul Islam .. 这是有效的,还有一个疑问我如何在 V21 以下应用涟漪效应
【解决方案2】:

当按钮具有可绘制的背景时,我们可以为前景参数添加波纹效果。检查下面的代码它是否适用于具有不同背景的按钮

<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"                                                             
android:background="@drawable/shape_login_button"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:text="@string/action_button_login"
 />

为波纹效果添加以下参数

android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"

【讨论】:

    【解决方案3】:
    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight" >
    
        <item
            android:id="@android:id/mask"
            android:drawable="@drawable/btn_rectangle"/>
        <item android:drawable="@drawable/btn_rect_states"/>
    
    </ripple>
    

    在 drawables 中尝试上面的代码,根据需要提供您自己的自定义可绘制背景。

    【讨论】:

    • 这很好,低于 v21 波纹不起作用,所以你需要为正常和按下状态创建按钮状态的选择器文件@PrithivDharmaraj ..如果我的回答对你有帮助,请接受或投票..跨度>
    【解决方案4】:

    另一个选项是创建一个可绘制的图层列表并将其设置为背景。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/button_background"/>
        <item android:drawable="?attr/selectableItemBackground"/>
    </layer-list>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多