【问题标题】:Android ripple for button not working按钮的Android波纹不起作用
【发布时间】:2017-09-02 13:12:18
【问题描述】:

我给按钮背景的涟漪效果不起作用。它只是切换颜色。设备版本是5.1.1。请帮帮我!!!

ripple.xml:

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/white"> <!-- ripple color -->

    <item android:drawable="@android:color/holo_blue_bright"/> <!-- normal color -->

</ripple>

【问题讨论】:

  • 我在上面的 xml 中没有看到任何错误。您可以尝试通过使用“@android:id/mask”和“@android:id/content”创建 2 个项目来更改波纹语法,因为它可能是与指定设备相关的错误。此外,如果您的项目支持低于 5.0,您可以尝试分析 APK 以查看该 apk 是否使用文件夹中的正确资源生成。

标签: android ripple


【解决方案1】:

你有没有这样尝试过

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:text="New Button" />

但是这里你不能改变涟漪效果颜色

【讨论】:

  • 为什么使用“?attr/selectableItemBackground”。尽管如此,我们分配了@drawable/ripple
  • @jobin "?attr/selectableItemBackground" 到你的视图的前景属性,如果它已经有背景连同 android:clickable="true"
  • 有关更多信息,请查看this link 如果它已经具有背景以及 android:clickable="true",请将 "?attr/selectableItemBackground" 添加到视图的前景属性中
【解决方案2】:

这里是一个示例,说明如何在使用波纹标签时设置正常的背景颜色。

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/white"> <!-- ripple color -->
    <item android:id="@android:id/background">
      <shape android:shape="rectangle">
        <solid android:color="@android:color/holo_blue_bright" /> <!-- normal color -->
      </shape>
    </item>
</ripple>

【讨论】:

    【解决方案3】:

    正确的做法是这样的——

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="#66ef5350"
        > <!-- ripple color -->
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="#66ef5350" />
                <corners android:radius="2dp"/>
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#ef5350" />
                <corners android:radius="2dp"/>
            </shape>
    
        </item>
    
    </ripple>
    

    您可以根据需要更改颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多