【问题标题】:How to change Android Button ripple colors while preserving other style properties?如何在保留其他样式属性的同时更改 Android Button 波纹颜色?
【发布时间】:2017-01-17 15:59:04
【问题描述】:

我想从默认材料设计风格继承所有Button 风格,并保留按下按钮时出现的背景颜色和阴影(所谓的高度)。我唯一想改变的是涟漪效果的颜色(当你触摸一个按钮然后移动你的手指时)。

如果我使用以下样式:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="android:buttonStyle">@style/MyTheme.Button</item>
</style>

<style name="MyTheme.Button" parent="Widget.AppCompat.Button.Colored">
  <item name="android:background">@drawable/btn_material</item>
</style>

而drawable/btn_material 是:

<ripple android:color="#ffff0000">
  <item android:id="@android:id/mask"
     android:drawable="@android:color/white" />
</ripple>

然后我的按钮失去了它的背景颜色和高度阴影。

有没有办法告诉按钮保留从父级继承的默认颜色和阴影,并且只用给定的颜色替换波纹颜色?本质上,我想实现类似android:backgroundRipple 属性的东西,它只会改变涟漪,而其他一切都完好无损。

【问题讨论】:

    标签: android themes material-design rippledrawable


    【解决方案1】:

    RippleDrawable 波纹= (RippleDrawable)view.getBackground();

    int[][] attr= new int[][] { new int[] { android.R.attr.state_enabled} }; int[] 颜色 = new int[] { Color.RED }; // 设置波纹颜色为红色

    ColorStateList 颜色 = 新颜色(状态,颜色); 波纹.setColor(颜色);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2010-10-30
      • 2021-06-21
      • 2015-03-29
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多