关键词:
【中文标题】Android,设置按钮的背景颜色会失去涟漪效果【英文标题】:Android, setting background color of button loses ripple effect 【发布时间】:2016-11-14 14:17:47 【问题描述】:在为 android 按钮添加颜色后,它会失去涟漪效果,让用户感觉就像是响应式点击。我该如何解决?我已经搜索了许多解决方案,但我找不到一个明确的不模棱两可的解决方案。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".ClockInOutFragment">
<AnalogClock
android:id="@+id/clock"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/date_and_time"/>
<RelativeLayout
android:id="@+id/date_and_time"
android:layout_
android:layout_
android:layout_margin="10dp">
<TextView
android:id="@+id/time_digits"
android:layout_
android:layout_
android:text="12:10"
android:textSize="45sp"/>
<TextView
android:id="@+id/am_pm"
android:layout_
android:layout_
android:layout_alignBaseline="@+id/time_digits"
android:layout_toRightOf="@+id/time_digits"
android:paddingLeft="3dp"
android:text="PM"
android:textSize="20sp"/>
<TextView
android:id="@+id/date"
android:layout_
android:layout_
android:layout_below="@+id/time_digits"
android:text="Mon, July 11"
android:textSize="22sp"/>
</RelativeLayout>
<!--Clock in and out buttons-->
<LinearLayout
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_weight="1"
android:background="#4CAF50"
android:gravity="center"
android:text="Clock In"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"/>
<!--Divider between the clock in and out button-->
<View
android:layout_
android:layout_
android:background="#B6B6B6"/>
<Button
android:id="@+id/textView4"
android:layout_
android:layout_
android:layout_weight="1"
android:background="#FF5252"
android:gravity="center"
android:text="Clock Out"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"/>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
【参考方案1】:您可以使用附加的波纹可绘制来添加波纹效果和背景颜色:
你的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<Button
android:id="@+id/button_connect"
android:layout_
android:layout_
android:layout_marginTop="20dip"
android:fontFamily="sans-serif"
android:text="Connect"
android:background="@drawable/ripple"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
ripple.xml(除了波纹效果之外,您还可以在此处添加背景颜色):
<?xml version="1.0" encoding="utf-8"?>
<!-- in drawable folder-->
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<!-- put your background color here-->
<solid android:color="@color/default_color" />
</shape>
</item>
</ripple>
【讨论】:
android如何区分哪一种是蒙版,哪一种是默认颜色? 按 ID。例如 - android:id="@android:id/mask" 这适用于更改菜单背景并保留涟漪效果【参考方案2】:执行此操作的一种非常简单直接的方法是将按钮的?attr/selectableItemBackground
设置为android:foreground
属性。以下 xml 完全有效且有效
<Button
android:id="@+id/btn"
android:layout_
android:layout_
android:background="@android:color/white"
android:foreground="?attr/selectableItemBackground"/>
【讨论】:
不错。但是,23 版不支持向后兼容。 如何改变效果的颜色? 这不适用于使用 style="@style/Widget.AppCompat.Button.Colored" 的弯曲按钮 结果是阴影有尖角,而不是弯曲的角。 适用于Widget.AppCompat.Button.Borderless
【参考方案3】:
不要改变 Button 的背景。换个主题。
<style name="ButtonGray">
<item name="colorButtonNormal">@color/gray</item>
</style>
在你的xml文件中
<Button
android:id="@+id/accept_button"
android:layout_
android:layout_
android:layout_weight="1"
android:text="@string/button_accept_group"
android:theme="@style/ButtonGray"/>
或者您可以将其添加到您的主应用主题中
<style name="AppTheme"
parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorButtonNormal">@color/primary_color</item>
</style>
并且不需要更改按钮背景。
如果你想要完全自定义的背景,你需要创建你的选择器。你可以在那里设置涟漪效应。
【讨论】:
【参考方案4】:只需使用:
android:backgroundTint="#4CAF50"
代替:
android:background="#4CAF50"
别忘了把你的Button
改成android.support.v7.widget.AppCompatButton
【讨论】:
这不适用于androidx.appcompat.widget.AppCompatButton
【参考方案5】:
MaterialButton没有答案,所以我把它放在这里。
对于 MaterialButton (com.google.android.material.button.MaterialButton),使用“backgroundTint”和“rippleColor”。
<com.google.android.material.button.MaterialButton
android:id="@+id/button_sign_in"
android:layout_
android:layout_
app:backgroundTint="@android:color/white"
app:rippleColor="?attr/colorControlHighlight"
?attr/colorControlHighlight
是默认的波纹颜色,你可以改变这个值。
【讨论】:
【参考方案6】:实际上,您可以使用drawables的<layer-list>
将涟漪效果与任何其他drawable结合起来。这也是 pre-lolipop 的通用解决方案:我已经在许多配置中对其进行了测试。
唯一的问题是当?selectableItemBackground
出现在<layer-list>
中时pre-lolipop 会崩溃,所以我们必须以编程方式创建LayerDrawable。
一个非常快速的简单解决方案如下所示:
为您的视图指定
android:background="?selectableItemBackground"
然后在代码中的任何地方创建 mySpecialDrawable 并执行此操作:
Drawable[] layers = mySpecialDrawable, getBackground();
setBackgroundDrawable(new LayerDrawable(layers).mutate());
请注意,LayeredDrawable 的 .mutate()
在这里是必不可少的!
当您已经拥有自定义 View 并且更喜欢扩展其功能和兼容性而不是添加额外的空 FrameLayout 作为父级时,更复杂的解决方案可能会很有用。
在attrs.xml里面放:
<resources>
<declare-styleable name="MyView">
<attr name="selectableBackground" format="reference"/>
<attr name="backgroundDrawable" format="reference"/>
</declare-styleable>
</resources>
然后在你的 View-descendant 类中:
private Drawable selectableBackground;
private Drawable backgroundDrawable;
public MyView(Context context, AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
try
TypedArray attributeArray;
attributeArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int id = attributeArray.getResourceId(R.styleable.MyView_selectableBackground, -1);
if (id != -1)
selectableBackground = ResourcesCompat.getDrawable(getResources(), id, context.getTheme());
id = attributeArray.getResourceId(R.styleable.MyView_backgroundDrawable, -1);
if (id != -1)
backgroundDrawable = ResourcesCompat.getDrawable(getResources(), id, context.getTheme());
constructBackground();
attributeArray.recycle();
catch (Exception e)
Log.e(this.toString(), "Attributes initialization error", e);
throw e;
void setSelectableBackground(Drawable drawable)
selectableBackground = drawable;
constructBackground();
void setDrawable(Drawable drawable)
backgroundDrawable = drawable;
constructBackground();
private void constructBackground()
if (selectableBackground != null)
if (backgroundDrawable != null)
Drawable[] layers = backgroundDrawable, selectableBackground;
setBackgroundDrawable(new LayerDrawable(layers).mutate()); // Both, using layers
else setBackgroundDrawable(selectableBackground); // Selectable only
else setBackgroundDrawable(backgroundDrawable); // Background only or null
我更喜欢这种方法,因为它没有像 android:foreground
属性这样的问题,它是 23+ 或在 FrameLayout 中包含可点击视图的额外开销。
【讨论】:
【参考方案7】:你应该使用样式
<style parent="Theme.AppCompat.Light" name="RaisedButtonGreen">
<item name="colorButtonNormal">@color/green</item>
<item name="colorControlHighlight">@color/greenLight</item>
<item name="android:textColor">@android:color/white</item>
</style>
这是最优解
<Button
android:layout_
android:layout_
android:text="Button with ripple for style"
android:theme="@style/RaisedButtonGreen"/>
【讨论】:
设置列表项背景颜色失去突出显示
...2【问题描述】:我创建了一个收件箱Activity,我正在镜像Android附带的默认邮件应用程序的一些功能。在Mail中,未读邮件的背景颜色比列表中其他项目的颜色要浅。我通过在我的适配器的getView方法中设置调用setBackgroundReso 查看详情
具有背景颜色的按钮的材质效果
...【发布时间】:2014-12-2810:58:03【问题描述】:我正在使用Androidv21支持库。我创建了一个带有自定义背景颜色的按钮。当我使用背景颜色时,波纹、显示等Material设计效果消失了(点击时的高度除外)。<Buttonstyle="?android:attr/butto... 查看详情
使用按钮背景颜色为我的按钮添加波纹效果?
...效果!我创建了一个按钮bgXML文件:(bg_btn.xml)<shapexmlns:android="http://schemas.android.com/apk/res/ 查看详情
摆脱涟漪效应 TextButton Flutter
...用FlatButton后,我不能再使用splashColor和highlightColor属性来设置用户按下按钮时的颜色。现在我必须使用其中一个新按钮,例如下面的TextButton,但我还没有找到任何具有背景颜色功能的 查看详情
选定状态覆盖的 Android 波纹效果
】选定状态覆盖的Android波纹效果【英文标题】:AndroidRippleEffectOverriddenbySelectedState【发布时间】:2015-07-2304:23:00【问题描述】:找了好久没找到答案……我有一个回收站视图,其中的项目在选择时具有红色背景和白色文本(之前... 查看详情
按下Android设置背景颜色按钮
】按下Android设置背景颜色按钮【英文标题】:Androidsetbackgroundcolorbuttonpressed【发布时间】:2014-06-2812:49:20【问题描述】:我想在按下事件时更改ImageButton的背景颜色。这就是我所做的:<?xmlversion="1.0"encoding="utf-8"?><shapexmlns:a... 查看详情
androidstudio中怎么将方形按钮设置成圆角以及渐变效果
...t;?xmlversion="1.0"encoding="utf-8"?><shapexmlns:Android="http://schemas.android.com/apk/res/android"><!--填充背景色--><solidandroid:color="#ffff00"/>//solid指的是背景颜色的设置<!--描边他需要2个参数,颜色第二... 查看详情
RadioButton 样式为普通按钮
...2017-03-1703:35:02【问题描述】:我正在尝试将我的RadioButton设置为普通按钮的样式,并支持按钮上的颜色更改,当单击其中一个按钮时会产生涟漪效果。现在发生的情况是,所有按钮都是灰色的,当它们被点击时它们会变成紫色,... 查看详情
(android学习)点击按钮button,更换背景颜色(代码片段)
一、效果录屏android,button按钮点击变色效果描述:按住button按钮颜色改变,松开按钮颜色恢复。二、代码实现drawable目录下:1、btn_nopress_shape.xml<?xmlversion="1.0"encoding="utf-8"?><shapexmlns:android="http:/... 查看详情
如何在 Android 上的 textview 或 imageview 上设置涟漪效果?
】如何在Android上的textview或imageview上设置涟漪效果?【英文标题】:HowtosetarippleeffectontextvieworimageviewonAndroid?【发布时间】:2016-02-0207:13:55【问题描述】:我想在AndroidStudio中的textview和imageview上设置涟漪效果。我该怎么做?【问... 查看详情
在 Android 和 iOS 上点击自定义按钮时,实现“突出显示效果”而不是“涟漪效果”
】在Android和iOS上点击自定义按钮时,实现“突出显示效果”而不是“涟漪效果”【英文标题】:Implementingthe"highlighteffect"insteadofthe"rippleeffect"toacustombuttonwhentappedonAndroidandiOS【发布时间】:2021-07-1905:43:24【问题描述... 查看详情
excel如何设置背景颜色
...只需要纯色填充时,请尝试应用图案或填充效果。选择要设置其格式的单元格或单元格区域。单击“开始”>“设置单元格格式”对话框启动器,或者按Ctrl+Shift+F。“字体”组中的对话框启动器在“填充”选项卡上,在“背景... 查看详情
android中的恒定涟漪效应
】android中的恒定涟漪效应【英文标题】:Constantrippleeffectinandroid【发布时间】:2020-05-3020:08:54【问题描述】:我想要做的是在LinearLayout的背景上产生恒定的涟漪效应。为什么?基本上,此LinearLayout表示正在观看此项目的实时用户... 查看详情
当网页同时设置了背景图片和背景颜色会是啥效果
网页背景颜色和背景图片,结构上都一样。一般放在body属性里,代码是background。在css里,背景颜色和图片可以同时使用!而往往,背景图片会优先运行。而图片覆盖不到的地方,会执行颜色代码。bodybackground:url(https://www.baidu.com... 查看详情
Android 按钮背景颜色
】Android按钮背景颜色【英文标题】:Androidbuttonbackgroundcolor【发布时间】:2013-08-0620:37:29【问题描述】:我正在尝试在我的应用中设置按钮的背景颜色,但我无法达到我想要的结果...我要设置的颜色是holo_green_light(#ff99cc00)。为了... 查看详情
在 Android Studio 中按住时如何更改按钮的背景颜色?
】在AndroidStudio中按住时如何更改按钮的背景颜色?【英文标题】:HowtochangethebackgroundcolorofabuttonwhenbeingheldinAndroidStudio?【发布时间】:2021-12-1921:24:52【问题描述】:因此,我在进行简单的代码修改时遇到了一些麻烦:我似乎无法... 查看详情
如何在保留其他样式属性的同时更改 Android Button 波纹颜色?
】如何在保留其他样式属性的同时更改AndroidButton波纹颜色?【英文标题】:HowtochangeAndroidButtonripplecolorswhilepreservingotherstyleproperties?【发布时间】:2017-01-1715:59:04【问题描述】:我想从默认材料设计风格继承所有Button风格,并保... 查看详情
更改圆形按钮android的背景颜色
】更改圆形按钮android的背景颜色【英文标题】:Changebackgroundcolorofcircularbuttonandroid【发布时间】:2014-11-0520:45:26【问题描述】:我在android中有一个按钮,我把它做成圆形。我还有一个xml,它会在按下时更改背景颜色,但在按下... 查看详情