【问题标题】:Using android:background@drawable and stateListAnimator on the same button causes problem在同一个按钮上使用 android:background@drawable 和 stateListAnimator 会导致问题
【发布时间】:2020-05-31 21:55:23
【问题描述】:

我有一个更新按钮,它有一个 Statelistanimator 来给用户一些点击反馈。 当我从 drawable 添加按钮背景时,动画不起作用/不显示,我无法解决这个问题。看起来背景就像按钮上的覆盖物(?)。我怎样才能让动画再次工作/显示?

背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorButton"/>
    <corners android:radius="30dp"/>
</shape>

动画

<?xml version="1.0" encoding="utf-8"?>
<!-- animate the translationZ property of a view when pressed -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="0"
                android:valueType="floatType"/>
        </set>
    </item>
    <item>
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="8"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

按钮

  <Button
            android:id="@+id/btnUpdate"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text=""
            android:stateListAnimator="@animator/update"
            android:clickable="true"
            android:textSize="16sp"
            android:focusable="true"
            android:layout_width="fill_parent"
            android:background="@drawable/button_background"

            />

【问题讨论】:

  • 找不到任何相关的帖子,有人吗?

标签: android xml android-studio android-layout android-animation


【解决方案1】:

这个“半”对我有用。给出我想要的效果,但动画是一个块,即使按钮是圆角的。

android:foreground="?android:attr/selectableItemBackgroundBorderless"

【讨论】:

    【解决方案2】:

    您需要为自定义背景添加内边距:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/colorButton"/>
        <corners android:radius="30dp"/>
        <padding
            android:bottom="@dimen/abc_button_padding_vertical_material"
            android:left="@dimen/abc_button_padding_horizontal_material"
            android:right="@dimen/abc_button_padding_horizontal_material"
            android:top="@dimen/abc_button_padding_vertical_material" />
    </shape>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多