【问题标题】:Make button animation not go over drawable background使按钮动画不超过可绘制背景
【发布时间】:2020-04-12 17:25:18
【问题描述】:

Top button: Not pressed, bottom button pressed

如何使按钮上的“默认”块动画点击 android 以适应设置的可绘制背景?可绘制背景为按钮提供圆角。

编辑:ripple.xml 有效,但覆盖按钮文本。

按钮:

        <Button
        android:id="@+id/about"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="20sp"
        android:text="test123"
        android:padding="5dip"
        android:textAllCaps="false"
        android:textColor="@color/settings"
        android:foreground="@drawable/ripple"
        />

可绘制的设置按钮

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="@color/colorWhite"/>
    <corners
        android:bottomRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"/>
</shape>
波纹.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
            <corners
                android:bottomRightRadius="15dp"
                android:bottomLeftRadius="15dp"
                android:topLeftRadius="15dp"
                android:topRightRadius="15dp"/>
        </shape>
    </item>
    <item android:drawable="@drawable/setting_button" />
</ripple>

【问题讨论】:

    标签: android xml android-layout android-button


    【解决方案1】:

    解决方案在于 RippleDrawable 的特殊遮罩层。您可以通过设置为@android:id/mask 的android:id 值指定遮罩层。 https://proandroiddev.com/rippling-rounding-and-android-pies-d5db5f4c2fc1 http://michaelevans.org/blog/2015/05/07/android-ripples-with-rounded-corners/

      <Button
        android:id="@+id/about"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@drawable/setting_button"
        android:foreground="@drawable/ripple"
        android:padding="5dip"
        android:text="test123"
        android:textAllCaps="false"
        android:textColor="@color/settings"
        android:textSize="20sp" />
    

    并从可绘制的波纹中删除&lt;item android:drawable="@drawable/setting_button" /&gt;

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
            <corners
                android:bottomLeftRadius="15dp"
                android:bottomRightRadius="15dp"
                android:topLeftRadius="15dp"
                android:topRightRadius="15dp" />
        </shape>
    </item>
    

    【讨论】:

    • 我不太明白如何实施此修复程序。 Michals 的解决方案似乎最简单,但 proandroiddev 表示这将打破 Oreo 上的按下状态。我正在为 API 23 及更高版本做准备。
    • 添加了带有 android:drawable=drawable 项的ripple.xml,用于制作圆角。没有效果。
    • 为可绘制的波纹创建新的资源文件,如 Michals 帖子中所述。然后在按钮的前景中引用此可绘制对象,而不是 selectableItemBackgroundBorderless
    • 我的错,看起来它现在可以工作了。但是现在圆形按钮样式的纯色覆盖了文本。
    • 纯色应该在背景中。波纹前景应该是苍白的,所以我不希望它覆盖文本。现在不确定。请发布代码
    猜你喜欢
    • 2020-06-19
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2017-05-11
    • 2019-05-16
    相关资源
    最近更新 更多