【问题标题】:Change Button background color with ripple使用波纹更改按钮背景颜色
【发布时间】:2017-08-13 12:49:35
【问题描述】:

将这段代码添加到我的 styles.xml 文件中,以将按钮的原色更改为绿色,即 colorPrimary。

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.Button" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/colorPrimary</item>
</style>

</resources>

然后我尝试将它与涟漪效果结合起来,因为我在另一篇堆栈帖子中读到您需要创建一个涟漪效果 xml 文件并将其用作按钮的背景。然后将按钮的样式设置为style.xml中详述的自定义样式。

我如何链接到编辑器中的按钮

我做错了什么。有没有更简单的方法可以同时为按钮和颜色添加波纹效果。我一直在尝试通过堆栈溢出帖子来解决这个问题,但每个解决方案似乎都不同。我使用的是 API 23。

【问题讨论】:

    标签: android android-layout android-view android-theme android-styles


    【解决方案1】:

    这可以使用ThemeOverlays 来实现。

    res/values/themes.xml:

    <style name="GreenButtonTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorButtonNormal">@color/green</item>
    </style>
    

    在你的 xml 中:

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button"
      android:theme="@style/GreenButtonTheme" />
    

    然后你会得到这个输出:

    详情请见this article

    【讨论】:

      【解决方案2】:

      你可以使用 setColorFilter():

      btn.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
      

      【讨论】:

        猜你喜欢
        • 2015-08-13
        • 2015-06-04
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多