我使用的是Android v21支持库。
我已经创建了一个自定义背景色的按钮。当我使用背景色时,材质设计效果如波纹,显示消失了(除了点击时的抬高)。
<Button
style="?android:attr/buttonStyleSmall"
android:background="?attr/colorPrimary"
android:textColor="@color/white"
android:textAllCaps="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
/>
下面是一个正常的按钮,效果很好。
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:text="Button1"
/>
我来这篇文章寻找一种方法,有一个背景颜色的我的ListView项目,但保持波纹。
我简单地添加了我的颜色作为背景,selectableItemBackground作为前景:
<style name="my_list_item">
<item name="android:background">@color/white</item>
<item name="android:foreground">?attr/selectableItemBackground</item>
<item name="android:layout_height">@dimen/my_list_item_height</item>
</style>
它就像一个魔法。我想同样的技巧也可以用在按钮上。祝你好运。
使用AppCompat(22.1.1+),你可以添加这样的样式:
<style name="MyGreenButton">
<item name="colorButtonNormal">#009900</item>
</style>
并通过应用样式来使用它:
<android.support.v7.widget.AppCompatButton
style="@style/MyGreenButton"
android:layout_width="match_width"
android:layout_height="wrap_content"
android:text="A Green Button"
/>
通过编程改变颜色,我发现更新颜色的唯一方法(在API 15或16上)是使用“背景色调列表”。它不会删除API 21设备上漂亮的径向动画:
ColorStateList colorStateList = new ColorStateList(new int[][] {{0}}, new int[] {0xFF009900}); // 0xAARRGGBB
button.setSupportBackgroundTintList(colorStateList);
因为button. setbackground(…)和button. getbackground ().mutate(). setcolorfilter(…)在API 15和16中不会像在API 21中那样改变按钮的颜色。
如果您可以使用第三方库,请查看traex/RippleEffect。它允许您仅用几行代码就可以向任何视图添加涟漪效应。你只需要在你的xml布局文件中,包装你想用com. anddexert .library. rippleview容器产生涟漪效应的元素。
作为额外的奖励,它需要Min SDK 9,这样你就可以在不同的操作系统版本之间保持设计一致性。
下面是一个摘自图书馆GitHub回购的例子:
<com.andexert.library.RippleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:rv_centered="true">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@android:drawable/ic_menu_edit"
android:background="@android:color/holo_blue_dark"/>
</com.andexert.library.RippleView>
你可以通过在RippleView元素中添加这个属性来改变波纹颜色:app:rv_color="@color/ my_fany_ripple_color "