android - 如何更改 Android 按钮波纹颜色,同时保留其他样式属性?

标签 android themes material-design rippledrawable

我想继承默认 Material 设计风格的所有 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 属性这样的东西,它只会改变波纹,而其他一切都完好无损。

最佳答案

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

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

ColorStateList color = new Color(状态, 颜色); 波纹.setColor(颜色);

关于android - 如何更改 Android 按钮波纹颜色,同时保留其他样式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39412615/

相关文章:

R - 将 ggplot 网格线放在前台

android - 禁用 CollapsingToolbarLayout 高程

angular-material - 在有棱角的 Material 调色板(例如 $mat-indigo 或 $mat-pink)中,A100、A200、A400 和 A700 用于什么?

android - 另一个 Activity 出现并覆盖屏幕尺寸的一半

带有导航组件的 Android 深层链接无法按预期工作

c++ - 将 Windows 主题应用于具有 WS_BORDER 样式的自定义控件

android-maven-plugin 和库项目

asp.net - CSS 更改,ASP.NET 主题中的更新未显示

java - 无法在 onPostExecute 内更新/重绘 listView

android - ViewPager 内的 WebView 故障 : scrolls like sliding a stack of cards