【问题标题】:Ripple Effect with custom drawable xml?带有自定义可绘制xml的涟漪效应?
【发布时间】:2017-10-21 17:38:17
【问题描述】:

我需要为所有控件添加涟漪效果以增加应用程序的活力。由于我的最低 api 级别为 18,所以我无法在可绘制的 xml 中使用<ripple>。此外,我所有的控件都有自定义可绘制为 XML。

我的自定义可绘制控件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp"/>
            <solid android:color="@color/border1"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"/>
            <corners android:radius="15dp"/>
            <solid android:color="@color/border2"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"/>
            <corners android:radius="15dp"/>
            <solid android:color="@color/border3"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp"/>
            <corners android:radius="15dp"/>
            <solid android:color="@color/border4"/>
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp"/>
            <corners android:radius="15dp"/>
            <solid android:color="@color/border5"/>
        </shape>
    </item>

    <!-- Background -->
    <item>
        <shape>
            <solid android:color="@color/menu_bg"/>
            <corners android:radius="15dp"/>
        </shape>
    </item>
</layer-list>

如果我使用android:background="?attr/selectableItemBackgroundBorderless" 控制涟漪效果效果很好。但问题是,我的所有控件都有自定义背景,所以我无法使用 attr/selectableItemBackgroundBorderless。

如何使用 attr/selectableItemBackgroundBorderless 和自定义可绘制背景?我不想用框架布局或任何其他布局包装控件.. 有没有其他办法?

等待回复,提前致谢

【问题讨论】:

    标签: android xml-drawable rippledrawable


    【解决方案1】:

    使用前台功能,实现可点击和可聚焦。

    <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"                             
       android:background="@drawable/custom_button_disable_fill"
       android:foreground="?android:attr/selectableItemBackground"
       android:text="Login"
       android:saveEnabled="true"
       android:focusable="true"
       android:textAllCaps="false"
       android:textColor="@color/black_bold_medium"
       android:textSize="@dimen/text_large"
       app:font_name="@string/sourcesanspro_semi_bold"/>
    

    【讨论】:

    • android:foreground 仅在 API > 23 时才有意义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2020-05-30
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2018-01-07
    相关资源
    最近更新 更多